Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if HTML style attribute exists with javascript

I'm trying to find out if a specific element has an inline style attribute or not: I'm sure there's an easy method to check this, but I can't seem to find it. I tried multiple things already including this:

var contentWrapper = document.getElementById("contentWrapper");
if(contentWrapper.style.display.toString=="")
alert("Empty");
else
alert("Not Empty");

Thank you for your help!

like image 963
Biffy Avatar asked Sep 05 '13 11:09

Biffy


People also ask

How do you check if data attribute exists in Javascript?

Use the hasAttribute() method to check if a data attribute exists, e.g. if (el. hasAttribute('data-example')) {} . The hasAttribute method returns true if the provided attribute exists, otherwise false is returned.

How do you know if an element has style?

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 attribute exists in HTML element?

To check if an HTML element has a specific attribute, you can use the hasAttribute() method. This method returns true if the specified attribute exists, otherwise it returns false .


1 Answers

if(contentWrapper.getAttribute("style")){
    if(contentWrapper.getAttribute("style").indexOf("display:") != -1){
        alert("Not Empty");
    } else {
        alert("Empty");
    }
}
like image 170
Praind Avatar answered Oct 12 '22 01:10

Praind