3

I have a problem with a click propagation event.

let say, I have a situation like this in a table:

<tr><td><div><span>some text</span></div></td></tr>

when I click on the span, the click is propagated to the parent. I would like the click to be propagated to the parent div but not to the parent td and tr.

when I do

$("td").click(function(e) {
        e.stopPropagation();
});

e refer to the span as that where the click originate. I'd like the click to start propagating but then stop at a specific object.

Thanks

EDIT:

we are using primefaces and the click event on the SPAN is automatically generated, so I don't really have access to it and I don't want to mess with primefaces code. The original click is on the span but I don't want it to propagate higher than a specific parent (in my example above the td). In fact I would like to stop the propagation on $(this) object, not on e

  • 3
    Are you trying to do a click event on the tr, but the span click event is also triggering it where you don't want it to? Does the span have a click event handler? There are multiple ways to solve this, but the right one to use will depend on what you are actually trying to do. – Kevin B Apr 30 '13 at 18:39
  • *"In fact I would like to stop the propagation on `$(this)` object, not on `e`"* that doesn't make sense. When the click handler on the td is fired, the event is currently at the `$(this)` element (which is the td). when you do `event.stopPropagation` there, it will prevent the event from propagating to the next parent of `$(this)`. – Kevin B Apr 30 '13 at 20:10

1 Answers1

4

Look into jQuery's on method. You can supply a parent element to target the event.

http://api.jquery.com/on/

$("td").on("click", "span", function(e){
  //...
});
Rick Viscomi
  • 8,180
  • 4
  • 35
  • 50
  • 1
    i always recommend the use of `.on()` rather than the shorthand because you can also combine multiple event bindings into the one encapsulation, rather than having bindings declared separately for each event. – PlantTheIdea Apr 30 '13 at 18:41
  • Just to clarify this answer a bit, this event would fire when the user clicks on any `span` within a `td` element. – Ryan Apr 30 '13 at 18:45
  • But would also be propagated to `
    ` before reaching ``?
    – Mitar Jan 12 '14 at 14:57