Saturday, February 21, 2009

jQuery's Trigger function

I found out about the trigger function in jQuery a while back and I love it. Using this we have the ability to "trigger" an event on all the matched element. 
For example, if there is an event-hander for a click event like this,

$("#testButton").click(
function(){
console.log("inside testButton click");
}
);
 
We can use the following code to execute the above event handler as if the click event happened (almost) in the browser.

$("#testButton").trigger("click");

The one cavet is if you trigger an event which uses the position (x,y coordinates) or keycode (enter key etc.,) you may get error, since those are not passed when using the trigger function.

No comments: