I am creating a label in the form of a bar that contains icons, this label has an click
associated event that, when activated, shows me a detail. Now, the problem arises when I want to give an event click
to the icon, which are contained within the mentioned tag and its event is not activated, but the event of the parent tag is activated.
I want to avoid moving the icon outside of the label and would like to know if there is a way to make its event fire by skipping the event of the containing label.
Esta es mi etiqueta con un evento click
la cual muestra un detalle en otra parte de la página:
<div class="bar-agent" onclick="show_detail_agent();">
<p class="agent-data"></p>
<span id="delete_agent" class="icon-delete">
<img src="img/trash.png" alt="">
</span>
</div>
What I'm trying to do is access the event click
of the span with id="delete_agent"
so that it removes this tag. But since it is contained within the with tag, class="bar-agent"
it does not allow me to access the span.
function _delete_agent(){
$(".content-agents .bar-agent-content .bar-agent").on("click","#delete_agent", function(){
console.log("aqui!");
});
}
Problem
When you want to use jquery selector for many classes, they should be separated by commas. For example, to select all elements of class
content-agents
AND all ofbar-agent-content
AND all ofbar-agent
:However, when selecting in this way, the event is associated with the
div
main one, something that will not allow us to prevent the propagation of the event or " bubble effect " (that the event escalates to parent elements).Solution
We can assign the event directly to the
<span>
one that contains the image like this:And, once it's bound in this way, to prevent bubble effect, the event has to return
false
.Code