How to correctly do something like the following without using jQuery.
$(document).ready(function(){
$("#someButton").click(function(){
alert("Hello");
});
});
Thanks.
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.
The easiest way is:
This will work on all browsers but note that with this approach only one event handler can be attached to an event.
Also notice that the
onloadevent is not completely equivalent to the jQuery’sreadyevent,onloadwill be fired when all the resources of the page are completely loaded (images, sub-frames, etc…), whilereadyfires as soon the DOM has been parsed.If you want to attach multiple event handlers, you can use the DOM Level 2 Standard
element.addEventListernermethod (orelement.attachEventfor IE)The simplest abstraction to get a cross-browser way to bind events is:
Then you can:
Be aware also that the
addEventListenerand the IE’sattachEventmethods have their differences, when you attach multiple handlers to an event withaddEventListenerthey will be fired in the same order, as the handlers were bound (FIFO),attachEventwill trigger the handlers in the contrary order (LIFO).Check an example here.