Dave's Brain

Browse - programming tips - jquery add event

Date: 2009apr1
Language: javaScript

Q.  How do *you* add a jQuery event?

A.  This this example we'll add an onChange() event.
The regular jQuery docs says this is the way to add an event:

$("#my_control").change(function() {
	alert('changed1');
});

Many people find this syntax weird.  I used a nameless function.
So you may wish to do it this way:

function my_control_changed() {
	alert('changed2');
}

$("#my_control").change(my_control_changed);

This way you can use the name of the function to provide a bit
of documentation.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: