Bug Tracker

Modify

Ticket #944 (closed enhancement: fixed)

Opened 7 years ago

Last modified 5 years ago

$().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:1 Changed 7 years ago by brandon

  • need set to Review

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.

comment:2 Changed 6 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 6 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 6 years ago by flesler

Hi Jorn, there is a plugin, it's called Bubble:  http://plugins.jquery.com/project/Bubble

comment:5 Changed 5 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to fixed

Works as desired in jQuery 1.3!

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.