user1127804
user1127804

Reputation: 35

stopPropagation is not working

It is not working,i need onclick base not id based code,please help me

<div onclick="favTheater(id)">
<img  src="#"/> 
</div>

<script type="text/javascript">
    function favTheater(id){
        alert("dsfdsf"+id);
        var e = window.event;
        e.stopPropagation();
        e.preventDefault();
        window.location.href="http://www.google.co.in/";
    }
    </script>

Upvotes: 0

Views: 158

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

A few problems with your code :

  • You're calling favTheater(id) on click but you didn't define an id,
  • you're using window.event, which exists only on IE. Note also that those old IE browsers didn't have stopPropagation (it came with IE9),
  • there is no reason to stop propagation or prevent default as you're replacing the page immediately with http://www.google.co.in/,
  • as noticed by jerome.s, there would be nothing to propagate anyway as your div isn't in a clickable element,
  • there isn't any default behavior to prevent (clicking on a div does nothing without specific handler), so preventDefault is useless.

So, your code is "not working", but it's hard to incriminate stopPropagation

Upvotes: 3

Related Questions