It sounds like you're wanting to change the background colour, which is the background-color
(or just background
), not color
. Here's a fiddle that does what you're trying to achieve.
I think you're mistaking how jQuery attaches event handlers, and what propagation is all about.
Take this example:
<div>Foo</div>
<div>Bar</div>
<div>Baz</div>
<script>
$('div').click(function(){
alert($(this).text());
});
</script>
What this does is attach a listener to each div
. When the div
is clicked, it fires the click
event, and any listeners attached to that element will fire.
A slightly more complex example:
<div id="divFoo">
<p id="pFoo">Foo</p>
</div>
<script>
$('#divFoo').click(function(){
alert('divFoo');
});
$('#pFoo').click(function(){
alert('pFoo');
});
</script>
Now divFoo
has a click handler, and pFoo
has one as well. Note that pFoo
's parent (divFoo
) has a click handler attached too.
Propogation is to do with the event 'bubbling up' the DOM tree. So, if I click pFoo
, the handler will fire. The click
event will then 'bubble up' to the parent div
, and the divFoo
handler will fire. $.stopPropagation
prevents this happening.
As an aside, if we have 2 handlers attached to a single element, then $.stopImmediatePropagation
will prevent any other handlers firing as well. The order the events fire in is essentially first come first serve, but not quite - there's another Q on Stack about that :).
This fiddle has a more complete example.
Note that the DOM tree is literally, a tree.
-- Root Div (level 0) #A
|--- Child P (level 1) #B
|--- Child P (level 1) #C
|--- Child Div (level 1) #D
|----- Child P (level 2) #E
|--- Child P (level 1) #F
Let's say I attach a handler to #E. The event will 'bubble' to #D (its parent), and then to #A (its parents parent). It wont touch #B, #C or #F, because they're siblings - not an ancestor.