Is there a way with jQuery to manually trigger an delegated event handler?
Take following example code:
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());
});
Trigger clicks on both buttons here!
Trigger a click on button "one" only.