Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery multiple selectors with window or document

I know how to use multiple CSS selectors with jQuery, but how can I bind an event listener to multiple selectors when one of them is an object, the document or the window object for instance.

The following doesn't work :

$('html, body', document).scroll(function () {
    if(Screen.detectScroll() === 'down') {
        self.hide();
    } else {
        self.show();
    }
});
like image 921
Romain Braun Avatar asked Jul 16 '14 22:07

Romain Braun


People also ask

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

Which selector is faster in jQuery?

Among all three selectors, the ID selector is the fastest selector because an ID of any HTML element will be unique within the web page and when a web page loaded, the browser will start searching for the element with a specified ID and an ID is unique, so the moment the browser finds the element with the specified ID, ...

Are jQuery selectors the same as CSS selectors?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

Can you use multiple CSS selectors?

A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator. There are four different combinators in CSS: descendant selector (space)


1 Answers

You can use jQuery's Add() to add selectors.

$('html, body').add(document).scroll(function () {
    if(Screen.detectScroll() === 'down') {
        self.hide();
    } else {
        self.show();
    }
});
like image 87
Romain Braun Avatar answered Sep 18 '22 13:09

Romain Braun