Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: How to check if the element has certain css class/style

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!

like image 200
0100110010101 Avatar asked Apr 17 '09 08:04

0100110010101


People also ask

How do you check if an element has CSS properties?

To check if an element's style contains a specific CSS property, use the style object on the element to access the property and check if it's value is set, e.g. if (element. style. backgroundColor) {} . If the element's style does not contain the property, an empty string is returned.

How do you check if an element has a specific class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");


1 Answers

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.

like image 173
Tamas Czinege Avatar answered Sep 30 '22 19:09

Tamas Czinege