189

I've got some links that I want to select class and id at the same time.

This is because I've got 2 different behaviours. When a class of links got one class name they behave in one way, when the same clas of links got another class name they behave differently. The class names are switch with jquery.

So I have to be able to select a links class AND id at the same time. Is this possible?

I've tried:

 $("a .save #country")

without any result.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
ajsie
  • 77,632
  • 106
  • 276
  • 381

6 Answers6

337

You can do:

$("#country.save")...

OR

$("a#country.save")...

OR

$("a.save#country")...

as you prefer.

So yes you can specify a selector that has to match ID and class (and potentially tag name and anything else you want to throw in).

Tintin81
  • 9,821
  • 20
  • 85
  • 178
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 44
    So basically its like : $("#a .b") means element with class b inside element with id a. $("#a.b") means element with class b and id a. The trick is the space between #a and .b – Bhumi Singhal Nov 14 '13 at 07:26
  • 13
    Must be careful that you use id selector before class otherwise it does not work. Example: $(".save#country")... does not return results. – AvgustinTomsic May 14 '14 at 13:18
54

Just to add that the answer that Alex provided worked for me, and not the one that is highlighted as an answer.

This one didn't work for me

$('#country.save') 

But this one did:

$('#country .save') 

so my conclusion is to use the space. Now I don't know if it's to the new version of jQuery that I'm using (1.5.1), but anyway hope this helps to anyone with similar problem that I've had.

edit: Full credit for explanation (in the comment to Alex's answer) goes to Felix Kling who says:

The space is the descendant selector, i.e. A B means "Match all elements that match B which are a descendant of elements matching A". AB means "select all element that match A and B". So it really depends on what you want to achieve. #country.save and #country .save are not equivalent.

Nikola
  • 14,888
  • 21
  • 101
  • 165
7

It will work when adding space between id and class identifier

$("#countery .save")...

Alex
  • 624
  • 1
  • 9
  • 12
  • 1
    actually this worked for me, as it was not working like $('#countery.save') so thank you! – Nikola Apr 13 '11 at 11:03
  • 1
    I've found sometimes it's picky about the space. like $(.selector > .item#id) works, but $(.selector > .item #id) doesnt. – Abe Petrillo May 10 '11 at 13:10
  • 25
    The space is the *descendant selector*, i.e. `A B` means "Match all elements that match B which are a descendant of elements matching A". `AB` means "select all element that match A and B". So it really depends on what you want to achieve. `#countery.save` and `#countery .save` are not equivalent. – Felix Kling Oct 14 '12 at 18:45
4
$("a.save, #country") 

will select both "a.save" class and "country" id.

o.k.w
  • 25,490
  • 6
  • 66
  • 63
3

In the end the same rules as for css apply.

So I think this reference could be of some valuable use.

getack
  • 172
  • 7
  • Could you summarize the contents of your reference? – krlmlr Oct 19 '12 at 00:06
  • Actually, according to http://api.jquery.com/category/selectors/ jQuery has some selectors of its own; also, it doesn't actually say that all CSS 1-3 selectors are supported... – SamB Oct 19 '12 at 00:09
  • @SamB You're right but, it does say it _borrows_ from CSS 1-3 AND adds its own. I still think the link to the W3C stuff is valid to this discussion. – akousmata Mar 25 '13 at 16:04
2

How about this code?

$("a.save#country")
Jess Stone
  • 677
  • 8
  • 21
Pekka
  • 442,112
  • 142
  • 972
  • 1,088