user1135534
user1135534

Reputation: 575

How to compare the attribute value

Is it possible to compare the attribute value in jQuery?

example

if( $(this).[ attribute value ] == "0")
{}
else
{}

Upvotes: 0

Views: 10041

Answers (4)

Guffa
Guffa

Reputation: 700232

You can get the attribute value using the attr method and compare it:

if ($(this).attr("attrname") == "0") {
  $(this).css('background', 'red');
}

You can also use a selector to filter out elements with a specific attribute value:

$(this).filter('[attrname=0]').css('background', 'red');

Upvotes: 0

Adil
Adil

Reputation: 148110

Try this

if( $(this).attr('myattribute') == "0")    
{
   //Your statements
}    
else    
{
   //Your statements
}

Upvotes: 1

mgraph
mgraph

Reputation: 15338

if($(this).attr("yourAttribute") == "0"){
    //do something
}

Upvotes: 1

VisioN
VisioN

Reputation: 145388

You can use attr method to get the attribute value:

if ($(this).attr("myAttr") == "0") {
    // "myAttr" value is "0"
} else {
    // "myAttr" value is not "0"
}

Upvotes: 5

Related Questions