0

I have a hidden div that opens up with a user clicks on the the title (Notifications). The "subpanel" div is hidden by default, but using jquery it will toggle open/close when a user clicks on the word Notifications.

How can I modify my jquery so that if a user clicks outside of the div that is toggled open (subpanel), this would also trigger the subpanel to close/hide ?

<div class="notiftitle" id="notiftitle">
<a href="#" class="alerts">Notifications</a>
 <div class="subpanel">
    <div id="title">New Notifications</div>
    <ul>
      <li>Data</li>
    </ul>
  </div>
</div>
$(document).ready(function(){

    //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
    $(".alerts").click(function(){
        $(this).toggleClass("notiftitleactive");
        $(this).toggleClass("active").next().slideToggle(50);
        return false; //Prevent the browser jump to the link anchor
    });


});
JoshB
  • 742
  • 1
  • 8
  • 12
Mark
  • 3,653
  • 10
  • 30
  • 62
  • 2
    You may better want to use evt.preventDefault() rather than return false: http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false – Janis Veinbergs Jul 03 '11 at 12:39

3 Answers3

2

It sounds like you need the "outerClick"-event. ouerClick jQuery plugin by Jon Kassen.

mqchen
  • 4,195
  • 1
  • 22
  • 21
1

You need to use the blur event:

$(".alerts").blur(function(){
    $(this).toggleClass("notiftitleactive");
    $(this).toggleClass("active").next().slideToggle(50);
});
JoshB
  • 742
  • 1
  • 8
  • 12
0

Try mouseout method.

SIFE
  • 5,567
  • 7
  • 32
  • 46