Thursday, April 28, 2011

Javascript OnclickOff

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.

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

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

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

    Or something like that anyway...


Post a Comment