I just started learning javascript and this is my first problem. I tried this method but it did not work. Please advise how I can use the switch as a function in events. Thanks in advance for your guidance
function lbl() {
switch (new Date().getDate()) {
case 0: {
document.getElementById('show').innerHTML = "sunday";
break;
}
case 1: {
document.getElementById('show').innerHTML = "monday";
break;
}
case 2: {
document.getElementById('show').innerHTML = "tuesday";
break;
}
case 3: {
document.getElementById('show').innerHTML = "wednesday";
break;
}
case 4: {
document.getElementById('show').innerHTML = "thursday";
break;
}
case 5: {
document.getElementById('show').innerHTML = "friday";
break;
}
case 6: {
document.getElementById('show').innerHTML = "saturday";
break;
}
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width= auto, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 id="show">show date! </h1>
<button onclick=lbl()>click me!</button>
<script src="js.js"></script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire