Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery click event propagation

I have a table with click events bound to its rows (<tr>). There are <a> elements inside those rows with their own click events assigned.

Problem is, when i click on the <a> element, it also fires the click event from the parent <tr>. I don't want this behavior; I just want to fire the <a> click event.

Code:

 // Event row TR

 $("tr:not(:first)").click(function() {
    $(".window, .backFundo, .close").remove();

    var position = $(this).offset().top;
    position = position < 0 ? 20 : position;

    $("body").append( $("<div></div>").addClass("backFundo") );
    $("body").append( $("<div></div>").addClass("window")
         .html("<span class=close><img src=Images/close.png id=fechar /></span>")
      .append( "<span class=titulo>O que deseja fazer?</span>"
              +"<span class=crud><a href=# id=edit>Editar</a></span>"
              +"<span class=crud><a href=# id=delete codigo=" 
              + $(this).children("td:first").html() 
              + ">Excluir</a></span>" )
       .css({top:"20px"})
       .fadeIn("slow") );

    $(document).scrollTop(0);
 });

 // <A> Element event

 $("a").live("click",function() { alert("clicked!"); });

Whenever you click the anchor it fires event from it parent row. Any ideas?

like image 831
ozsenegal Avatar asked Feb 11 '10 12:02

ozsenegal


People also ask

What is click event propagation?

Event propagation is a way to describe the “stack” of events that are fired in a web browser. In our table example above, clicking on the a tag is the first event that we will fire, but there are other events too. To understand that concept, you must understand that the elements on a web browser are nested.

What is event propagation in jquery?

Definition and Usage. The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed. Tip: Use the event. isPropagationStopped() method to check whether this method was called for the event.

How do I stop click event propagation?

To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.

What is the difference between event stopPropagation and event stopImmediatePropagation?

stopPropagation allows other event handlers on the same element to be executed, while stopImmediatePropagation prevents this. stopPropagation and stopImmediatePropagation prevents event handlers later in the capturing and bubbling phases from being executed.


1 Answers

You have to stop event bubbling. In jQuery you can do this by

e.stopPropagation();

in the onclick event of the anchor tag.

$("a").live("click",function(e){alert("clicked!");e.stopPropagation();});

Edit

See this post

jquery Event.stopPropagation() seems not to work

like image 151
rahul Avatar answered Sep 22 '22 04:09

rahul