Can I create a selector with multiple elements using jquery nearest ()

I have a listener for click events, from which I need to exclude some elements.

Now my list is growing, so I'm looking for the best way to "stratify" several elements in the selector.

This is what I have:

$(document).on('click tap', function(event) {               
    if ($(event.target).closest('div:jqmData(panel="popover")').length > 0 ||
        $(event.target).closest('div.pop_menuBox').length > 0 ||
        $(event.target).closest('.toggle_popover').length > 0 ) ||
        $(event.target).closest('.ui-selectmenu').length > 0 {
          return; 
    }
    // do stuff
});

Is there a better way to exclude these elements?

Thanks for the help!

+5
source share
3 answers

You can specify a CSS selector, which means: you can use a comma to specify two or more selectors:

if($(event.target).closest('div:jqmData(panel="popover"), div.pop_menuBox, .toggle_popover, .ui-selectmenu').length > 0) {
    return; 
}
+19
source

Use a comma.

if ($(event.target)
     .closest('div:jqmData(panel="popover"), div.pop_menuBox, .toggle_popover, .ui-selectmenu').length > 0) {
     return; 
}
+2
source

According to the jquery documentation, you can provide more than one selector for the closest: http://api.jquery.com/closest/

0
source

All Articles