CelestAspect
CelestAspect

Reputation: 27

Can I select in JQuery id & class own by the same element?

I create a system who add & remove class, the goal is to change the css with in Jquery but only if the button had an "X" class AND "Y" id. Is it possible ?

            if ($(".NotSelect, #reponse1")) {
                $(this).css("background-color","green")
                $(this).prop('disabled', false);       
            } else if($(".Select, #reponse1")){
                $(this).css("background-color","red")
                $(this).prop('disabled', true);            }

Upvotes: 0

Views: 43

Answers (2)

Yuvraj Durgesh
Yuvraj Durgesh

Reputation: 56

Use .hasClass("YourClass") method of jquery

if($('#response1').hasClass('NotSelected')){
    $(this).css("background-color","green")
    $(this).prop('disabled', false);
}else{
    $(this).css("background-color","red")
    $(this).prop('disabled', true);
}

Upvotes: 1

Pranav Rustagi
Pranav Rustagi

Reputation: 2721

What I can understand is that, how to make changes on an element only if it has an id, say "X", and a class, say "Y".

Do do that, you gotta do something like this :

if($(this).is('#X.Y')){
    $(this).css("background-color","green");
    $(this).prop('disabled', false);
}
else{
    $(this).css("background-color","red");
    $(this).prop('disabled', true);
}

NOTE : I am assuming here that this is referring to the button element you want to make check on. And don't forget to replace "X" and "Y" with the desired id and class.

Upvotes: 0

Related Questions