I have this code:
var StateTextBox = document.getElementById("State");
var IgnoreTextBox = document.getElementById("Ignore");
var PlayButton = document.getElementById("Play");
if(document.getElementById("Play").onclick == true)
StateTextBox.value = "Happy";
<input TYPE="button" VALUE="Play with Pogo" id="Play" onClick="changestate();"/>
onclick attribute identifies what should happen when the user clicks this particular element. In your case, you're asking that a function be ran; when the function runs, you can rest assured that the button was clicked - that is after all how the function itself got put into motion (unless you invoked it some other way).
Your code is a bit confusing, but suppose you had two buttons and you wanted to know which one was clicked, informing the user via the
You can test this code live at http://jsfiddle.net/Y53LA/.
Note how we add event-listeners on our
ignoreButton. This permits us to keep our HTML clean (no need for an
onclick attribute). Both of these will fire off the
changeState function anytime the user clicks on them.
changeState function we have access to an
event object. This gives us some details about the particular event that took place (in this case, the
click event). Part of this object is the
target, which is the element that was clicked. We can grab the
id property from that element, and place it into the
value of the
Here is the adjusted HTML:
<input type="button" value="Play with Pogo" id="play" /> <input type="text" id="state" /> <input type="button" value="Ignore with Pogo" id="ignore" />