Select an element with jquery that has multiple classes

$(".status").attr("rel"); 

.status can contain up to three other additional classes. .online, .away, .offline

if i do this:

 $(".status.online").attr("rel"); 

it works when the status is online as a second class, but not when it has been changed, how can I do this, and no matter what other classes it has?

+4
source share
5 answers

If an element has several classes, you can still select it using any of these classes. Therefore, your selector is correct, you do not need to change anything.

 $(".status").attr("rel"); 

If you think that a class can change, and it can have any or these classes ( online, offline, away ) along with status, try this.

 $(".status.online, status.offline, .status.away").attr("rel"); 
+8
source

You can filter the results for additional classes. I think this is what you want to do. This basically says get me all the elements with the status class. Then take these results and return to me those that also have online, away or offline.

 $(".status").filter('.online, .away, .offline'); 
+2
source

We can use..

 $('.status').filter('.online, .offline, .away').each(function(){ alert($(this).html()); });' 

It will warn the HTML div.

+1
source

What about

 $("status.online, .status.away, .status.offline"); 

or

 $('.status').filter('.online, .offline, .away'); 

http://jsfiddle.net/nJcSp/1/

0
source

I think something like this.

$(".status:regex(class, (online|offline))").attr("rel");

Trevor: I can’t comment yet, like this: what you have done will look for 3 different classes that return more than one object, and not one with 3 classes

0
source

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


All Articles