views:

47

answers:

4

I have a setup like this:

    <div id="container">
       <div id="close">Close</div>
    </div>

Then in my jquery I have this:

$("#container").live("click",function(){
   changeTabs();
});

$("#close").live("click", function(){
   closeTabs();
});

The problem with that is that when you click the close div it fires both events. I am guessing it is because the second div overlays the first one. So how can I cancel or stop the first event from firing?

A: 

This might work:

$("#container:not(#close)").live("click",function(){
   changeTabs();
});

$("#close").live("click", function(){
   closeTabs();
});

Using the not selector.

Steve Wortham
+4  A: 

Check out event.stopPropagation()

From the jQuery docs:

Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

In your case, this might look like

$("#close").live("click", function(evt){
   evt.stopPropagation();
   closeTabs();
});
Daniel LeCheminant
+1 spot on, also, `return false` will stopPropagation and preventDefault (in a jQuery event handler).
karim79
it did not work. it still ran the changeTabs() function
ngreenwood6
A: 

I'm not sure why you want to use a div as a close click event, are you okay to use an input button instead?

<div id="container">
       <input type="button" id="close" value="Close"/>
</div>

$("#container").live("click",function(){
    changeTabs();
});

$("#close").live("click", function(){
    closeTabs();
    return false;
});
ticallian
input button will not work.
ngreenwood6
A: 

Ok so the method that worked was under the closeTabs() function I did return false; and that seemed to fix it

ngreenwood6