How to check if a button is clicked or not in prototype JavaScript?
$('activateButton').observe('click', function(event) {
alert(hi);
});
The code above is not working.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
With this button:
Use this:
Tested and works, here.
You might want to encase it in a
document.observe('dom:loaded', function () { })thingy, to prevent it executing before your page loads.Also, just an explanation:
The single dollar sign in Prototype selects an element by its id. The
.observefunction is very similar to jQuery’s.onfunction, in that it is for binding an event handler to an element.Also, if you need it to be a permanent ‘button already clicked’ thingy, try this:
And then, if you want to test if the button has been clicked, then you can do this:
This may help if you are trying to make a form, in which you don’t want the user clicking multiple times.
How one may do it in jQuery, just for a reference:
Note that, the jQuery code mentioned above could also be shortened to:
jQuery is better in Prototype, in that it combines the usage of Prototype’s
$and$$functions into a single function,$. That is not just able to select elements via their id, but also by other possible css selection methods.How one may do it with plain JavaScript:
Just for a complete reference, in case you need it.