4

When I press a div on my page, a popup appears. When you click the div again, the popup disappears. When you click outside of the div, the popup disappears - all looking good so far.

The problem is, when I click the popup, I want it so that the popup and it's children are clickable (they're links inside an unordered list), but I can't seem to get that to work. Here is my code:

        $(function () {
            $('#messageNotification').click(function (event) {
            $("body").one('click', function () {

                jQuery(".userInfo").fadeOut();
            });
            event.stopPropagation();
        });


          <body>
            <div id="messageNotification"><div class="notif same">0</div></div>
            <div id="messages">
                <ul class="userInfo" style="">
                    <li></li>
                </ul>
            </div>
             
          </body>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jezorama
  • 347
  • 1
  • 3
  • 13

1 Answers1

7

If I'm understanding you correctly, you can filter by selector. For example:

$('body').on('click', 'a', function(){ ... });

This would bind the click event to all elements matching the selector a.

If you want to bind the click to all elements not matching your popup, you could do something like:

$('body').on('click', ':not(#popup)', function(){ // code to dismiss popup here });

See the jquery documentation for :not() and .on() for more info...

Neal Pisenti
  • 526
  • 1
  • 3
  • 7