95

I have a div:

<div class="test" id="someElement" style="position: absolute"></div>

Is there any way to check if the certain element:

$("#someElement") 

has a particular class (in my case, "test").

Alternately, is there a way to check that en element has a certain style? In this example, I'd like to know if the element has "position: absolute".

Thank you very much!

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
0100110010101
  • 6,469
  • 5
  • 33
  • 38
  • 14
    not sure if it'sd the question or the answer that's wrong, but the question has style="test", and the answer looks for class="test" – wheresrhys Apr 17 '09 at 09:26
  • 1
    My guess is, the question is wrong, since he marked the wrong answer as right; but regardless, after looking at the votes, I think most people have stumbled in here looking for the correct answer to this wrong question... er, if that makes any sense... The answer from DrJokepu, I mean. It's why I'm here at least. – BrainSlugs83 Jun 19 '12 at 07:04

6 Answers6

296

CSS Styles are key-value pairs, not just "tags". By default, each element has a full set of CSS styles assigned to it, most of them is implicitly using the browser defaults and some of them is explicitly redefined in CSS stylesheets.

To get the value assigned to a particular CSS entry of an element and compare it:

if ($('#yourElement').css('position') == 'absolute')
{
   // true
}

If you didn't redefine the style, you will get the browser default for that particular element.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
  • 3
    There's no way to do it with a selector? – BrainSlugs83 Jun 19 '12 at 07:02
  • with this you can see what is the actual property, what I need to know is if this property is being set directly or is it inherited from css style / browser... is that possible? – ante.sabo Sep 20 '12 at 11:24
  • Doesn't seem to work for the scenario if the DOM changes/css value of an object changes after page loads. Any idea how to get it to watch css changes after the DOM loads? – Collarbone Jul 19 '16 at 06:01
  • @Collarbone You could always use setTimeout() and clearTimeout() to delay the call for a second or so once the document has loaded. – probie Aug 20 '18 at 21:34
61
if($('#someElement').hasClass('test')) {
  ... do something ...
}
else {
  ... do something else ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13
if ($("element class or id name").css("property") == "value") {
    your code....
}
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
Sandeep Gupta
  • 380
  • 4
  • 14
6

Or, if you need to access the element that has that property and it does not use an id, you could go this route:

$("img").each(function () {
        if ($(this).css("float") == "left") { $(this).addClass("left"); }
        if ($(this).css("float") == "right") { $(this).addClass("right"); }
    })
kelseedev
  • 109
  • 1
  • 2
1

Question is asking for two different things:

  1. An element has certain class
  2. An element has certain style.

Second question has been already answered. For the first one, I would do it this way:

if($("#someElement").is(".test")){
    // Has class test assigned, eventually combined with other classes
}
else{
    // Does not have it
}
Mario Vázquez
  • 717
  • 10
  • 9
1

i've found one solution:

$("#someElement")[0].className.match("test")

but somehow i believe that there's a better way!

0100110010101
  • 6,469
  • 5
  • 33
  • 38