Search code examples
jqueryjquery-selectorsjquery-eventsmouseupjquery-trigger

Selecting all elements except for div not working with jQuery


I'm trying to select all elements in the document except for the #private_chat_menu element and attach a mouseup trigger function to them. However, it runs the function regardless of whether I click a select box inside the #private_chat_menu element or not. Here is the code:

<script>
$("*:not(#private_chat_menu > *)", "body").mouseup(function(e)
{
    var chat_user_container = $('#private_chat_menu');
    var chat_container = $('#chat-wrapper');

    if (chat_container.css("visibility") == 'visible' && chat_user_container.is(':visible'))
    {
        chat_user_container.hide();
    }
});
</script>

<div id="chat-wrapper">
   <div id="private_chat_menu">
      <select id="chat_user_select" name="chat_user_select">
         <option value="">Select Assigned User</option>
         <option value="1">...</option>
         <option value="2">...</option>
      </select>
   </div>
</div>

JSFiddle: http://jsfiddle.net/q0ky2f56/


Solution

  • Given your requirement it would perform better (and have simpler logic) if you attached a single event handler to the document and interrogate the element which raised the event. If it was the #private_chat_menu, or a child of it, do no work. Something like this:

    var $chat_user_container = $('#private_chat_menu');
    var $chat_container = $('#chat-wrapper');
    
    $(document).on('mouseup', function(e) {
      var $target = $(e.target);
      if ($target.is($chat_user_container) || $target.closest($chat_user_container).length)
        return;
    
      if ($chat_container.is(':visible') && $chat_user_container.is(':visible')) {
        $chat_user_container.hide();
      }
    });