views:

40

answers:

5

Im using the following click function on an element:

$('#extras').toggle(function() {
                    $('div#extras').show(); 
                    $('div#extras').stop().fadeTo('fast', 1);
                }, 
                function() {
                    $('div#extras').stop().fadeTo('fast', 0, function() { 
                                                             $(this).hide(); 
                                                             } );
                }
);

extras is a div, with many children in it. Some are buttons. Now every time I click one of the buttons, it makes the parent div along with the children disapear.

How do I make it so the click function only fires if the parent is clicked, and not all of its children?

Thanks :)

A: 

Try to use the children() method.

$('div#extras').children().show();
Yves M.
+1  A: 

If you hide the parent, you WILL hide the children (decendants) by default.

just for completeness: the selector for the parent of the current element is $(this).parent();

Mark Schultheiss
cat
A: 

If a div disappears, all of its content disappears. If you have to make the parent disappear without the children, you should place the children outside in the DOM, and if needed, visually place them inside using CSS.

streetpc
Thats not what I meant sorry I don't think I was clear enough. I explained more in the question above :)
cat
A: 

When you click on a element the click event will be fired for all parent elements. This can be stopped by returning false from the event handler of the children. So you need to have event handlers for all children that shouldn't fire the click event of your div and return false from them. Something like this:

$("#id-of-button-inside-your-div").click(function() {
    //Do whatever you need to do
    return false;
});
Mattias Jakobsson
Well the good thing is 'extras' is a tool bar so everything in it basically does something :) thanks a lot! Ill try it out now
cat
Yepp that's working! I just have to go into all of my functions and add it to the rest now. Thanks :)
cat
Oh wow its getting very complicating cause I have to do it to so many things... If theres any other way you could think that would be great
cat
A: 

Try this:

$('div#extras').find('*').click(function(event){ event.stopPropagation(); });

This will make all the elements inside the div to ignore the click event from the div.

NicolasT
That is working somewhat.It does cancel the function from the extras div for all of the children, but it cancels it completely, so even if you click just the extras div it doesn't do anything.Thanks though!
cat
Then you can use that and then rebind the click event on the inner div's, or maybe use live in the inside events.
NicolasT