Creating switch

Hi, I am trying to create a switch with an input field where you can write a day and then push a button and it should print the number of the day. Example, you write Monday and it should say “1” and so on.
I can’t understand what to do or change to make the code run? I have written this code inside an editor that’s why it looks like what it does.

Here is what I have done this far…


<html>
<body>

<h2>write a day</h2>

<input type="text" id="funka">

<button type="button"
onclick="myFunction()">
Check the number of weekday</button>

<p id="funka"></p>


<script>


switch (new Date().getDay()){


 case 0:
 dag="Söndag"
 document.getElementById("funka");
  break;
 case 1:
 dag="Måndag"
 document.getElementById("funka");
  break;
  case 2:
  dag="Tisdag"
  document.getElementById("funka");
  break;
  case 3:
  dag="Onsdag"
  document.getElementById("funka");
  break;
  case 4:
  dag="Torsdag"
  document.getElementById("funka");
  break;
  case 5:
  dag="Fredag"
  document.getElementById("funka");
  break;
  case 6:
  dag="Lördag"
  document.getElementById("funka");

}
document.getElementById("funka").innerHTML=" ";

</script>
</body>
</html>

Hello!

You have a couple of errors. For instance, You cannot have duplicate id attributes:

<input type="text" id="funka">

<button type="button"
onclick="myFunction()">
Check the number of weekday</button>

<!-- Here "funka" is already defined, so change this id or the input -->
<p id="funka"></p>
  • The function myFunction does not exist.
  • The switch will only execute one time (when the page first loads).
  • The switch, as it is right now, will only give you today’s day number, not the input.

A simple way to fix the switch would be:

switch (day.toLowerCase()) {
  case 'monday':
    return 1;
  case 'tuesday':
    return 2;
  case 'wednesday':
    return 3;
  case 'thursday':
    return 4;
  case 'friday':
    return 5;
  case 'saturday':
    return 6;
  case 'sunday':
    return 7;
}

Of course, this is just one way to do it. Look at this sample and see if it helps You.