tags:

views:

100

answers:

6

I want to stop propagation of this div's onclick event to the document? When the user click on the "div", both alerts appear: 1) the div's alert and 2) the document's alert. I want to suppress the document alert.

I know how to do it using addEventListener, but is there another way to to do it? The problem below is that I don't know how to get ahold of the event -- I tried "event = element.onclick", shown below, but that doesn't work. How do I get the event?

<head>
<script>
  function showMenu(element) {
      alert("div clicked");
      event = element.onclick;  // HOW TO GET HOLD OF THE EVENT?
      // Don't propogate the event to the document
      if (event.stopPropagation) {
          event.stopPropagation();   // W3C model
      } else {
          event.cancelBubble = true; // IE model
      }
  }

  document.onclick = function() {
      alert('document clicked');
  };
</script>
</head>

<body>
  <div id="foodmenu" onclick="showMenu(this);">Click inside this div</div>
  or click outside the div.
</body>
A: 

Add the onclick to the body element.

Aaron Harun
+4  A: 

Change your function definition to include the event:

function showMenu(event, element) {
  alert("div clicked");
  // Don't propogate the event to the document
  if (event.stopPropagation) {
      event.stopPropagation();   // W3C model
  } else {
      event.cancelBubble = true; // IE model
  }
}

Then change the call to pass in the event:

div id="fooddmenu" onclick="showMenu(event, this);">Click inside this div</div>
Hooray Im Helping
Terrific! I tried your fix and it works! Thank you so much.A follow-up, if you please. I had spent hours searching and reading quirksmode and chapters of books and couldn't find this answer anywhere. Where does the authoritative reference state that the first argument of an event handler is the event? (Is "event" a reserved keyword here?)
douglas.kramer
It doesn't have to be the first argument. The order is determined by the event setup in the HTML, e.g. if you used `onclick="showMenu(this, event);"` then the second argument would be the event data.
Ben Voigt
What Ben said :)
Hooray Im Helping
Great! So it appears 'event' is a reserved argument, like 'this'.I'm still curious what website is the authoritative reference for the event parameter. I've looked at w3c.org and mozilla.
douglas.kramer
A good place to start: http://javascript.crockford.com/
Hooray Im Helping
This answer I wrote to a related question is relevant: http://stackoverflow.com/questions/2196299/why-the-subtle-cross-browser-differences-in-event-object
Tim Down
Thanks, Tim, that's helpful.Hooray, I'm actually looking for the definitive reference that states that event is a reserved identifier passed into event handlers, not some developer's handbook.BTW, I looked at crockford, but it says "Some browsers pass an event object to event handlers as a parameter. Microsoft chose instead to put the event object in a global event variable." On the contrary, I have code where IE 7 does accept an event passed to an event handler.
douglas.kramer
Hooray Im Helping
+1  A: 

Try EventListeners:

html:

<div id="fooddmenu">Click inside this div</div>or click outside the div.​​​​​​​​​​

js:

function showMenu(e) {
    alert("div clicked");
}

document.onclick = function() {
    alert('document clicked');
};

window.onload = function(){
    document.getElementById("fooddmenu").addEventListener("click", function(e){
        showMenu(this);
        e.stopPropagation();

    });
};
digitalFresh
Thank you. This is a clean alternative.
douglas.kramer
A: 
$("div").click(function(){
  ...
  ...
  ...

  return false; //this will stop the further propagation of the event
});
Ashit Vora
I looked into this, and it seems "return false" does NOT stop further propagation of the event. It simply prevents the default action from taking effect, which is different. For example, if you click on a link, "return false" would prevent the browser from following the link, but would not prevent the event from bubbling up to the document where it could fire document.click=function() {alert("hi");};
douglas.kramer
A: 

Douglas, It does stop the event from getting bubbled up.

Check this out http://jsbin.com/ahoyi/edit

here, if you comment the alert statement, it will show 2 alerts on clicking the smaller box else only one.

Hope this helps.

Ashit Vora
Thanks for showing me that. I"m unclear what the dollar sign '$' does. I tried replacing it with document.getElementById("parent") but that give an error: "Object #<an HTMLDivElement> has no method 'click'"
douglas.kramer
A: 

well, that's a jquery code. $("#id") same as document.getElementById("id")

.click function is same as addEvent("click", function() { ... } );

so basically both the functions there are click handlers for Parent and Child DIVs.

Observe the output by commenting / uncommenting the "return false;" statement.

Hope that helps.

By the way, sorry for that "$" confusion.

Ashit Vora