Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 706
frequent Avatar asked Mar 20 '13 09:03

frequent


People also ask

How do you trigger a callback function?

It is possible to trigger callback functions at the top, middle, or end of any other function. By moving the position where the callback is triggered, the result will change whenever we trigger function B . Callback functions will execute as long as you call them inside another function.

How does Callback work in jQuery?

To prevent this from happening jQuery provides a callback function for each effect method. A callback function is a function that is executed once the effect is complete. The callback function is passed as an argument to the effect methods and they typically appear as the last argument of the method.

What is trigger click in jQuery?

trigger( "click" ); As of jQuery 1.3, . trigger() ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false from the handler or calling the . stopPropagation() method on the event object passed into the event.

How do you trigger a click element?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


1 Answers

When you call trigger, the bound event handler is immediately executed, so you don't need any callback. Just use

$input.trigger('click'); runtests(); 
like image 62
Denys Séguret Avatar answered Sep 20 '22 15:09

Denys Séguret