tags:

views:

39

answers:

2

Hi there,

I have a div that I want to trigger a function when I click on it (easy enough using onclick) that shows child divs that have been hidden. I also want to make it so that when you click anywhere else on the document after the child divs have been shown, it will hide the child divs.

I am not sure of the best way to approach this, thanks in advance.

+2  A: 

Put an onclick handler on the body element. This handler will contain the code to hide the div.

Mark Byers
right but if you click on the div, it toggles the body as well.
Walt
You can put a click handler in the div that does nothing except "event.cancelBubble=true;" then the body handler is never invoked.
Mark Byers
+3  A: 

Using jQuery:

$(function(){
    $(this).click(handleGeneralClick);
});

function handleGeneralClick(evt)
{
    if ($(evt.currentTarget).attr('id') == 'my_div')
    {
        // Show child div's
    }
    else
    {
        // Hide child divs
    }
}

Or something like that anyway...

Darrell Brogdon