Alex Rivera | Logout

How to run a callback function on a jQuery trigger("click")?

Asked 2013-03-20T09:14:49.637
34

I need to trigger a custom event in the callback of a trigger call, but I can't get it to work.

I tried this:

var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);

function runtests () {
    console.log("clicked the input");
};
$input.trigger('click', runtests()); 

and this:

var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
$input.trigger('click', function(){
    console.log("clicked the input");
}

Neither of which works.

Question:
How do I get a callback function to run when I'm triggering a click on an element?

Edit
Report

1 Answer

0

The Trigger doesn't have a callback function - you can better re-write your code to use the .on method.

$("body").on("click", "#click", function() {
   alert("Clicked!"); 
});

JSFiddle.

answered 2013-03-20T09:20:26.890

Your Answer