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?
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.
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.
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.
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.
When you call trigger
, the bound event handler is immediately executed, so you don't need any callback. Just use
$input.trigger('click'); runtests();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With