views:

106

answers:

3

If I have...

<div id="parent" onmouseover="doSomething()">
    <div id="child" onmouseover="doSomethingElse()">
    </div>
</div>

How can I make it so that doSomething() is not executed when I'm hovering over the child? I only want doSomethingElse() to execute when I'm hovering over the child, and I only want doSomething() to execute when I'm hoving over the parent.

Whenever I hover over the child, both functions execute. Any ideas?

Matt

+1  A: 
function doSomethingElse(e) {
// your function body

e.stopPropagation() 

}
Marwan Aouida
doesn't work in IE
Christoph
Isn't that a jQuery function?
Macha
no it is pure javascript: https://developer.mozilla.org/En/DOM/Event.stopPropagation
Marwan Aouida
@Marwan: nitpick - it's not JS, it's DOM level 2, ie it's specified by W3C, not ECMA
Christoph
Thanks for correcting :)
Marwan Aouida
re-nitpick. It's a JS /binding/ to a DOM level 2 specification. Marwan never said it was specified by ECMA.
Matthew Flaschen
re-re-nitpick: in common usage, JavaScript either refers to ECMAScript or an actual browser-specific implementation thereof; you chose to include the browser API in this definition, I didn't; if you use the term correctly (ie for Mozilla's ES implementation), the DOM API is independant of JS: Rhino (an implementation of JS1.7) doesn't support the DOM, as it's not part of the language spec
Christoph
yes I had to say it is pure javascrit/DOM
Marwan Aouida
+1  A: 

Change the attribute value to

onmouseover="doSomethingElse(event)"

and change doSomethingElse() to

function doSomethingElse(e) {
    e.stopPropagation && e.stopPropagation();
    e.cancelBubble = true;
    // ...
}
Christoph
the cancelBubble is not a standard property. It is better to use the stopPropagation method
Marwan Aouida
@Marwan: if you want to ignore IE users, go ahead, but check the browsers of your target audience beforehand
Christoph
So long as IE ignores the standards it deserves like treatment. Eye for an eye, and all that jazz. ;-)
The Wicked Flea
Henceforth, The Wicked Flea shall be known as a champion of the biblical approach to web-design and follower of The Book of Mozilla
Christoph
A: 

Hi.

I didn't test it, but there should be a few ways to do it.

Please check event bubbling : it's different in IE and Firefox but still: www.quirksmode.org/js/events_order.html