Is there a way with jQuery to manually trigger an delegated event handler?
Take following example code:
<div class="container"> <input type="button" value="Hello"> <span class="output"></span> </div> <script> $('.container') .on('click', '[type=button]', function(e) { $(e.delegateTarget).find('.output').text($(this).val()); }) .find('[type=button]').triggerHandler('click'); </script>
(Online: http://jsfiddle.net/TcHBE/)
I was expecting that this would work, and text "Hello" would appear in the span without actually clicking the button, but it doesn't.
I'm using e.delegateTarget
inside the handler, because the .ouput
element won't be in a known relationship to the button, other than some place inside the .container
. That is why I'm using a delegated event handler in the first place.
Update:
Also I'm using triggerHandler
, because the event has a default behaviour in the real code I don't want to trigger. (In the real code the event is the custom event hide
of the Bootstrap Modal plugin, but I don't actually want to hide the modal when triggering the event handler on page load).
I could extract the handler into a named function and call it directly, but due to the use of e.delegateTarget
, that would make the how construct more complicated.
jQuery delegate() Method Use the on() method instead. The delegate() method attaches one or more event handlers for specified elements that are children of selected elements, and specifies a function to run when the events occur.
$( "#foo" ). 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 .
The delegate() Method in jQuery is used to add one or more event handlers to the specified element that are children of selected elements and are also used to specify a function to run whenever the event occurs.
You could create an Event object manually and set the target
property accordingly to trick jQuery into thinking the event bubbled up.
var c = $('#container'); c.on('click', '[type=button]', function(e) { $(e.delegateTarget).find('span').text($(this).val()); }); var event = jQuery.Event('click'); event.target = c.find('[type=button]')[0]; c.trigger(event);
http://jsfiddle.net/PCLFx/
I know, this question is ancient but as I was stumbling over it while looking for an answer to another problem I thought I might as well share my slightly simpler approach here.
The idea is to simply create the event on the desired target element directly: $(target_selector).trigger(event_type)
or - even shorter for standard events like "click" - do $(target_selector).click()
, see my little fiddle below:
$(function(){ $('.container').on('click','button',function(){ console.log('delegated click on '+$(this).text()); return false; }); $('#other').click(e=>$('.container button').trigger('click')); $('#clickone').click(e=>$('.container .one').click()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="container"> <button type="submit" class="one">click 1</button> and another chance to click here on <button class="two">click 2</button>. </form><br> <div id="other">Trigger clicks on both buttons here!</div><br> <div id="clickone">Trigger a click on button "one" only.</div>
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