-2

I have two div, Outer Div and Inner Div, both the div attached with click event. whenever i click inner div it alert outer div also, i want to prevent the event handler of Outer div when i click the Inner Div in Javascript

Here is my Javascript

function funOne()
{
   //event.stopPropagation();
   alert('Outer Div')
}

function funTwo()
{
  //event.stopPropagation();
  alert('Inner Div')
}

Here is the DEMO

It worked yesterday.
  • 4,507
  • 11
  • 46
  • 81
Dinesh Kanivu
  • 2,551
  • 1
  • 23
  • 55

4 Answers4

2

Using stopPropagation should work to stop propagation from the child div to the parent div, but you functions are lacking an event object.

function funOne(e)
{
    e.stopPropagation();
    alert('Outer Div')
}

function funTwo(e)
{
    e.stopPropagation();
    alert('Inner Div')
}

You also need to pass the event when you call the function:

<div class="first" onclick="funOne(event)">
    <div class="second" onclick="funTwo(event)"></div>
</div>

Note that stopPropagation only works for child/parent situations. If the one div merely appears to be inside another div, stopPropagation does nothing.

woz
  • 10,888
  • 3
  • 34
  • 64
  • 1
    `e` is going to be `undefined`. – dfsq Apr 14 '15 at 12:17
  • @dfsq Why do you think so? – Sebastian Simon Apr 14 '15 at 12:18
  • @Xufox OP provided a demo, it's very easy to test. – dfsq Apr 14 '15 at 12:19
  • @Xufox Because, according to the jsfiddle in the question, the functions are called without parameters. – JJJ Apr 14 '15 at 12:19
  • @Juhana Event handlers always have the event object as the first argument (if you do it with `addEventListener` anyway). – Sebastian Simon Apr 14 '15 at 12:20
  • @Xufox Yeah, but the OP isn't doing it with `addEventListener`. – JJJ Apr 14 '15 at 12:21
  • @Xufox Are you sure :) just open a fiddle and try it. note, that it's inline event handlers, not addEventListener. – dfsq Apr 14 '15 at 12:22
  • @woz: Can you please elaborate on `If the one div merely appears to be inside another div, stopPropagation does nothing.` – n4m31ess_c0d3r Apr 14 '15 at 12:26
  • @Navendu After checking the jsFiddle from the OP, I guess this isn't really relevant to his situation. I meant that the inner div has to be a child HTML element of the parent; you can't use CSS to position one div inside another and expect `stopPropagation` to work. – woz Apr 14 '15 at 12:27
1

Check here you missed to defined event.

function funOne(e)
{
    e.stopPropagation();
    alert('Outer Div')
}

function funTwo(e)
{
    e.stopPropagation();
    alert('Inner Div')
}
.first{width:600px; height:600px; background:yellow}
.second{width:100px; height:100px; background:orange; position:relative; top:50%; left:50%; margin-left:-50px; margin-top:-50px}
<div class="first" onclick="funOne(event)">
 <div class="second" onclick="funTwo(event)"></div>
</div>

Check Fiddle.

ketan
  • 19,129
  • 42
  • 60
  • 98
1

Updated your fiddle check it out :

<div class="second" onclick="funTwo(event)"></div>

function funTwo(event)
{
event.stopPropagation();
alert('Inner Div')
}

https://jsfiddle.net/7r4hhzo0/4/

Kushal
  • 1,360
  • 6
  • 16
0
<script>
  function funOne(e)
  {
     e.stopPropagation();
    alert('Outer Div')
  }

   function funTwo(e)
    {
      e.stopPropagation();
     alert('Inner Div')
  }
   </script>

You need to call these function like onclick="funOne(event);" and onclick="funTwo(event);"

Nimesh.Nim
  • 388
  • 2
  • 9