Pranav C Balan
Pranav C Balan

Reputation: 115222

OnChange event handler not working

I'm using onchange event handler with an input element. I know it's simple, I just added an onchange attribute. But it's not at all working also not showing any kind of error in console.

<script type="text/javascript">
  function select(a) {
    console.log(a);
    document.getElementById("demo").innerHTML = a;
  }
</script>
<p id="demo"></p>

<input type="checkbox" onchange="select('XYZ')">

Upvotes: 2

Views: 1037

Answers (3)

svidgen
svidgen

Reputation: 14282

select is a reserved word in JavaScript (sort of).

<script type="text/javascript">
  function _select(a) {
    document.getElementById("demo").innerHTML = a;
  }
</script>
<p id="demo"></p>

<input type="checkbox" onchange="_select('XYZ')">


Note that, as the link above states, select isn't technically a reserved word (in JavaScript). It can be used in general for variable names and functions. But, browser implementations do refuse to bind DOM events directly to functions that share DOM property names. And, I'm not yet sure where this restriction or conflict is explicitly named in the specs ... or if it's just an unhappy accident.

Upvotes: 5

Arpan Shah
Arpan Shah

Reputation: 169

<script type="text/javascript">
  function select1(a) {
    console.log(a);
    document.getElementById("demo").innerHTML = a;
  }
</script>

<input type="checkbox" onchange="select1('XYZ')" name="xyz" value="xyz">
<p id="demo"></p>
you should change your function name becouse there is collision occurs that's why it's not working try above

Upvotes: 1

JJWGOURAV
JJWGOURAV

Reputation: 11

Javascript has a list of reserved keywords which cannot be used as function names or variable names.

For a complete list, check this link: http://www.w3schools.com/js/js_reserved.asp

Upvotes: 1

Related Questions