I’m trying to get value of button which is clicked, or determine which button was clicked.
From code below, I’m getting all values but I just want the button that was clicked.
<script type="text/javascript">
function GetCheckedFruits () {
var elements = document.getElementsByName ("fruit");
for (var i=0; i < elements.length; i++) {
if (elements[i].click) {
alert ("The " + (i+1) + ". button is click");
}
}
}
</script>
HTML:
<input id="Text1" name="fruit" type="button" value="apple" onclick="GetCheckedFruits ()" /><br />
<input id="Text1" name="fruit" type="button" value="banana" onclick="GetCheckedFruits ()" /><br />
<input id="Text1" name="fruit" type="button" value="blackberry" onclick="GetCheckedFruits ()" /><br />
Where am I going wrong?
If you want to alert the message when the button is clicked, you would have to use an event like this:
EDIT:
In the case you are working with checkboxes and are trying to get which fruits where selected, this will work: