Opened 17 years ago
Closed 15 years ago
#944 closed enhancement (fixed)
$().trigger + event propogation
Reported by: | anonymous | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | |
Component: | event | Version: | |
Keywords: | event trigger propogation | Cc: | |
Blocked by: | Blocking: |
Description (last modified by )
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 (5)
comment:1 Changed 16 years ago by
need: | → Review |
---|
comment:2 Changed 16 years ago by
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 16 years ago by
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 15 years ago by
Hi Jorn, there is a plugin, it's called Bubble: http://plugins.jquery.com/project/Bubble
comment:5 Changed 15 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
Works as desired in jQuery 1.3!
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.