5

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>

2
  • Is there an error in the console? Commented Oct 26, 2016 at 18:20
  • @LucasBaizer nope, no error in console (using Dreamweaver) Commented Oct 26, 2016 at 18:21

3 Answers 3

12

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>

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't it possible for him to use the lang as long as he wraps it into a private function? Of course this is a better fix, but if he really wanted to use that function name?
it was exactly as you said, changing the function name solved the problem. Thank you, I didn't think about the global lang function..
When I run the snippet I still got both cases, it is a SO snippet problem ?
@Marcs What do you mean you got both cases? The snippet works fine. What browser are you using?
I'm sorry I was expecting only "Italiano selezionato" as result in the snippet and I didn't saw the alert function.
3

The problem is here. Around the onclick event handler there is used with, which allow you to use all global attributes (including lang) there. So it tries to access the global lang property.

So change you function name to anything else, but no attributes names

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

<script>
  function alertLang(language) {
  switch (language) {
    case "it-IT":
      alert("Italiano selezionato");
      break;
    case "en-US":
      alert("English selected");
      break;
  }
}
  </script>

But it will work if you add it as a event handler in Javascript

<p><a href="#">English</a></p>

<script>
  function lang() {
      alert("English selected");
  }
  
  document.querySelector('p').onclick = lang;
  </script>

1 Comment

Not the global lang.
0

When you assign JavaScript code to the onclick handler, it is executed in a special scope where the properties of the element are available directly as is it were running inside a with (element) { } block.

In your example, the lang variable corresponds to the lang attribute of the element. Other examples that will cause the same error include id, title, style and href. Think of it as executing the following code:

function lang() {
}

with(element) {
    lang("en-US");
    // type error since lang resolves to element.lang
    // instead of the outer lang
}

You can simply namespace your function:

var MyFancyFunctions {
    lang: function {
    }
};
<a onclick="MyFancyFunctions.lang('en-US')">English</a>

Or just remove the ambiguity:

<a onclick="window.lang('en-US')">English</a>

References:

The internal raw uncompiled handler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.