Browse - Programming Tips - How do *you* add a jQuery event?

Date: 2009apr1
Language: javaScript
Library: jquery

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.