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?

qiao
  • 17,941
  • 6
  • 57
  • 46
Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • 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 Jan 31 '12 at 03:12
  • Yeah, that's what I thought, but I actually just realized it was an error in my code for the reason not working. – Dylan Cross Jan 31 '12 at 03:14
  • possible duplicate of [How can I select an element with multiple classes?](http://stackoverflow.com/questions/1041344/how-can-i-select-an-element-with-multiple-classes) – TylerH Feb 20 '15 at 20:08

5 Answers5

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");
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
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');
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
1

We can use..

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

It will alert the div HTML.

MikkoP
  • 4,864
  • 16
  • 58
  • 106
Arun Manjhi
  • 175
  • 9
0

How about

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

or

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

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

Trevor
  • 11,269
  • 2
  • 33
  • 40
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