jQuery: select an element's class and id at the same time?

309,916

Solution 1

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).

Solution 2

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.

Solution 3

It will work when adding space between id and class identifier

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

Solution 4

How about this code?

$("a.save#country")

Solution 5

In the end the same rules as for css apply.

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

Share:
309,916

Related videos on Youtube

ajsie
Author by

ajsie

please delete me

Updated on August 07, 2020

Comments

  • ajsie
    ajsie almost 4 years

    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.

  • krlmlr
    krlmlr over 11 years
    Could you summarize the contents of your reference?
  • SamB
    SamB over 11 years
    Actually, according to 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...
  • akousmata
    akousmata about 11 years
    @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.
  • amindfv
    amindfv over 10 years
    Is the problem that you said "countery" instead of "country"?
  • Bhumi Singhal
    Bhumi Singhal over 10 years
    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
  • AvgustinTomsic
    AvgustinTomsic almost 10 years
    Must be careful that you use id selector before class otherwise it does not work. Example: $(".save#country")... does not return results.