Block function call in parent div from a child div

2

It happens that I have a parent div which calls a function sending in this two parameters (one of these is 0 since it will be fixed) and within this div I have several divs which call the same function but with the two dynamic parameters :

<div onclick='abrirUbicacion(ID1,0);'>
  <div onclick='abrirUbicacion(ID1, ID2);'></div>
  <div onclick='abrirUbicacion(ID1, ID2);'></div>
</div>

When I click on any of the internal divs it calls the function correctly sending both parameters well but immediately executes the function of the main div by sending the 0 as a parameter. What is this about? and how can I fix it?

    
asked by Santiago Muñoz 22.05.2017 в 19:30
source

1 answer

3

You must use the event.stopPropagation() method to prevent the event from spreading from child to parent.

Example:

function abrirUbicacion(ID1, ID2) {
 event.stopPropagation();
 alert("Ubicacion: "+ID1+", "+ID2);
}
<div onclick='abrirUbicacion(0,0);'>Ubicaciones
  <div onclick='abrirUbicacion(1,1);'>Ubicacion 1</div>
  <div onclick='abrirUbicacion(2,2);'>Ubicacion 2</div>
</div>
    
answered by 22.05.2017 / 20:04
source