Anonberg
Anonberg

Reputation: 41

Button Function Working Without Click

<script type="text/Javascript">
  function phase2Function(){
    document.getElementById("0").onclick = buttonChangeFunction();
    }
  function buttonChangeFunction(){
    document.getElementById("1").disabled = "true";
    }
</script>
<button id="0" onclick="numberFunction(0)">0</button>
<button id="1" onclick="numberFunction(1)">1</button>

I have a problem with the above code that makes the "buttonChangeFunction" work without me clicking 0. The project im working on is a calculator, where after you press a math operator, it starts phase2Function. Edit: The Question Has been Answered, but I was wondering what to do if I actually want to put something in between the parentheses of buttonChangeFunction?

Upvotes: 0

Views: 1175

Answers (1)

enhzflep
enhzflep

Reputation: 13099

You need to remove the parenthesis after buttonChangeFunction in phase2Function.

What you are trying to do is make buttonChangeFunction fire each time the button with the ID of '0' is clicked.

Instead, what you're actually doing, is calling buttonChangeFunction, getting back an undefined result (since the function doesn't return anything) and then setting the click handler of the id=0 element to this undefined result.

So, simply change: document.getElementById("0").onclick = buttonChangeFunction();

to

document.getElementById("0").onclick = buttonChangeFunction;

Upvotes: 1

Related Questions