Jquery validation for multiple classes (all must be present) in an element

Hi, I am trying to check if an element has classes that I find ex:
<span class="foo bar me"></span>

I want to check if a class is fooalso present mefor this element, so I used this:

$('body span').hasClass('me', 'foo')

this foo and me are stored in an array, but this one does not work:

var arrayName = ['me', 'foo'];
$('body span').hasClass(arrayName)

the classes that I find are dynamic, so they are stored in an array, how do I pass a dynamic array, like this one 'me', 'foo', to the hasClass function?

Note: an array can have 2-4 values.

I had my work here: https://jsfiddle.net/pcbttntk/

Thanks at Advance.

Update: $('body span').hasClass('me', 'foo') does not work, thanks to Brett and Roko, he only checks the first class passed to him.

+4
2

:

<span class="foo bar me"></span>

me foo:

$('span').is(".foo.me");

, .join(), ".foo.me", :

var classes = ['me', 'foo'];
$('span').is("."+ classes.join("."));   

:

var classes = ['.me', '.foo']; // (Notice the `.`)
$('span').is(classes.join(""));

, .hasClass('me', 'foo') .hasClass()

+4

jQuery .hasClass() ( docs).

.is():

var class1 = 'me';
var class2 = 'foo';

arrayNames = [class1, class2];

console.log($('body span').is(arrayNames.map(function(className) {return '.'+ className;}).join(',')));

JSFiddle.

0

Source: https://habr.com/ru/post/1626090/


All Articles