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

.status can have up to three other additional classes added to it. .online, .away, .offline

if I do this:

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

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

3
  • 1
    Your first example, $(".status").attr("rel"); is the answer to your question. That gets the element no matter what other classes are present. Are you saying you want that element, but only if it ALSO has one of the others?
    – mrtsherman
    Commented Jan 31, 2012 at 3:12
  • Yeah, that's what I thought, but I actually just realized it was an error in my code for the reason not working. Commented Jan 31, 2012 at 3:14
  • possible duplicate of How can I select an element with multiple classes?
    – TylerH
    Commented Feb 20, 2015 at 20:08

5 Answers 5

8

If the element has multiple classes you can still select it just by using any of those class. So your selector is correct you don't have to change anything.

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

If you think that the 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");
1
  • Yeah, I realized it was just an error I had with my code for it not working how it should have. Commented Jan 31, 2012 at 3:16
2

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

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

We can use..

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

It will alert the div HTML.

0

I think is something like this.

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

Trevor: I can't comment yet, so: What you did will search for 3 differents classes, which return more than one object, not a single one with the 3 classes

0

How about

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

or

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

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

2
  • Well, I suppose I could do something like: $(".status.online, .status.away,.status.offline") but I'm hoping there's a better way of doing this. Commented Jan 31, 2012 at 3:13
  • In what other way would you want to do this? :P
    – uadnal
    Commented Jan 31, 2012 at 3:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.