Ben
Ben

Reputation: 35

How can I use multiple variables (selectors) with is() Method

var container   = $(".container"),
    background  = $(".background");

// ...

// Inside an each() method – check if is container OR background
if (this.is(container, background)) { // Not working
    // do something
}

// I know I could do it like this ...
if (this.is(container) || this.is(background)) {
    // do something
}

// ... but I want it shorter

Is it even possible to check against multiple selectors as variables with is()?

Upvotes: 2

Views: 62

Answers (1)

Parag Bhayani
Parag Bhayani

Reputation: 3330

you could use this selector

this.is('.container, .background')

So basically, 1)

if (this.is('.container, .background')) {
    // do something
}

2)

var elements = ('.container, .background');
if (this.is(elements))  {
}

3)

if (this.is([container[0], background[0]])) {
    // do something
}

4)

if (this.is(container.add(background))) {
    // do something
}

5)

var temp = container.add(background);
if (this.is(temp)) {
    // do something
}

Upvotes: 2

Related Questions