MineII CrafterII
MineII CrafterII

Reputation: 49

Why can’t I have a function named “click” at global scope?

The values of the buttons should change when you click on them

function click(objekt) {
  if(objekt.value == 1)
    objekt.value = 0;
  else
    objekt.value = 1;
}
<input type="button" value="1" onclick="click(this);">
<input type="button" value="0" onclick="click(this);">
<input type="button" value="1" onclick="click(this);">

Please explain how to get the value on onclick event?

Upvotes: 0

Views: 65

Answers (3)

Minar_Mnr
Minar_Mnr

Reputation: 1405

Name of your function is the culprit here. you can not name your function name "click", it's restricted . Change your function name, it will work.

function click123(objekt) {
  if (objekt.value == 1)
    objekt.value = 0;
  else
    objekt.value = 1;

}
<input type="button" value="1" onclick="click123(this);">
<input type="button" value="0" onclick="click123(this);">
<input type="button" value="1" onclick="click123(this);">

Upvotes: 3

Shiladitya
Shiladitya

Reputation: 12181

Here you go with the solution https://jsfiddle.net/o0zxqq0u/

clickFunc = function(objekt) {
    if(objekt.value == 1)
        objekt.value = 0;
    else
        objekt.value = 1; 
}

**HTML**
<input type="button" value ="1" onclick="clickFunc(this);" />
<input type="button" value ="0" onclick="clickFunc(this);" />
<input type="button" value ="1" onclick="clickFunc(this);" />

Upvotes: 0

Steve Woods
Steve Woods

Reputation: 227

Try removing the spaces on

<input type = "button" value ="1" onclick="click(this);">
<input type = "button" value ="0" onclick="click(this);">
<input type = "button" value ="1" onclick="click(this);">

to

<input type="button" value="1" onclick="click(this);">
<input type="button" value="0" onclick="click(this);">
<input type="button" value="1" onclick="click(this);">

Also, what are the values when you change them? Try printing them out. That might help you find what you're looking for. Does it actually change to 1 or 0? Also, I know you don't need curly brackets on if statements if there is only one statement but try with curly brackets.

Upvotes: 0

Related Questions