Tony33 Tony33 - 1 month ago 6
Javascript Question

javascript switch case not taking variable

I have a simple html link , in which I call a JavaScript function onClick and try to switch the value taken in. It's not working.

Here is my code:



function lang(language) {
switch (language) {
case "it-IT":
alert("Italiano selezionato");
break;
case "en-US":
alert("English selected");
break;
}
}

<p><a href="#" onClick="lang('en-US');">English</a></p>
<p><a href="#" onClick="lang('it-IT');">Italiano</a></p>




Answer

Don't use lang for your function name, the browser is using it already.

function xlang(language) {
  switch (language) {
    case "it-IT":
      alert("Italiano selezionato");
      break;
    case "en-US":
      alert("English selected");
      break;
  }
}
<p><a href="#" onClick="xlang('en-US');">English</a></p>
<p><a href="#" onClick="xlang('it-IT');">Italiano</a></p>

Comments