Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually trigger a delegated event with jQuery?

Tags:

jquery

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.

like image 448
RoToRa Avatar asked Oct 04 '12 09:10

RoToRa


People also ask

Which methods facilitate event delegation in jQuery?

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.

How can add trigger click event in jQuery?

$( "#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 .

What is the delegate () method in jQuery?

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.


2 Answers

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/

like image 193
Prinzhorn Avatar answered Sep 21 '22 08:09

Prinzhorn


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>
like image 25
Carsten Massmann Avatar answered Sep 23 '22 08:09

Carsten Massmann