Ticket #944 (closed enhancement: fixed)
$().trigger + event propogation
| Reported by: | anonymous | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | event | Version: | |
| Keywords: | event trigger propogation | Cc: | |
| Blocking: | Blocked by: |
Description (last modified by joern) (diff)
Test case:
<div><span><a href="#">test link</a></span></div>
<script type="text/javascript">
$('div, a')
.bind('click', function(e){
alert('current: '+ this.nodeName +', target: '+ e.target.nodeName);
});
$('a').trigger('click');
</script>
Result:
current: A, target: A
Expected result:
current: A, target: A current: DIV, target: A
Change History
comment:2 Changed 5 years ago by flesler
Ok, the demo says: $('div, a') not $('div a'). Note the comma. I assume that's a typo ?
This is solved with event delegation, instead of $('div a')
use $('div').click(function(e){
if( e.target.nodeName == 'A' )
......................;
});
comment:3 Changed 5 years ago by joern
- Description modified (diff)
Ariel, it doesn't look like its a typo. To make it more clear:
<div><span><a href="#">test link</a></span></div>
<script type="text/javascript">
$('div').bind('click', function(e){
alert('current: '+ this.nodeName +', target: '+ e.target.nodeName);
});
$('a').trigger('click');
</script>
So this is about event bubbling for custom events, which isn't implemented. I agree with Brandon that a plugin implementation would be helpful for a start.
comment:4 Changed 5 years ago by flesler
Hi Jorn, there is a plugin, it's called Bubble: http://plugins.jquery.com/project/Bubble
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Perhaps this could be done by creating a "propagate" method that if the event wasn't stopped the method would search the parents for handlers of the same type. It could be done but perhaps best done via a plugin and might turn out to be too slow.