16

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!

4
  • 1
    possible duplicate of How can I check if attribute exists or not in Jquery Commented Sep 5, 2013 at 11:35
  • 1
    getAttribute('style') Commented Sep 5, 2013 at 11:35
  • Also, be careful when calling functions to make sure you add parenthesis. display.toString == "" is comparing the function , when you want to compare the return value of the function. It should be display.toString() == "". (Not that you need .toString() here anyway, as display is already a string, but it's another reason why your code might fail) Commented Sep 5, 2013 at 12:40
  • 2
    The two duplicates linked provide solutions in jQuery which is not explictly asked for by the user; therefore, this is not a duplicate of either of those questions. Commented Sep 5, 2013 at 13:44

5 Answers 5

19
if(contentWrapper.getAttribute("style")){
    if(contentWrapper.getAttribute("style").indexOf("display:") != -1){
        alert("Not Empty");
    } else {
        alert("Empty");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is not so perfect for commentarized strings like this "/* font-size: 24px; */". In this case it works as if font-size exists.
8
if(!contentWrapper.getAttribute("style"))

OR

if(contentWrapper.getAttribute("style")==null || 
   contentWrapper.getAttribute("style")=="")    

the above lines will work for you (anyone can be chosen).

In second solution:

first check watches if style attribute is present in the element, 2nd check ensures that style attribute is not present as an empty string e.g. <div id="contentWrapper" style="">

Complete code is given below:

var contentWrapper = document.getElementById("contentWrapper");
if(contentWrapper.getAttribute("style")==null || contentWrapper.getAttribute("style")=="")
alert("Empty");
else
alert("Not Empty");

http://jsfiddle.net/mastermindw/fjuZW/ (1st Solution)

http://jsfiddle.net/mastermindw/fjuZW/1/ (2nd Solution)

3 Comments

The double check is unecessary, since both, null and '' are falsey. Also, developer.mozilla.org/en-US/docs/Web/API/element.hasAttribute would be better.
2nd check is to make sure that style attribute is not present as an empty string e.g. <div id="contentWrapper" style="">
I know, but what I mean is that Boolean('') is false. So if (!contentWrapper.getAttribute('style')) { //empty } works for null or ''.
2

The style object has a length property which tells you if the element has any inline styles or not. This also avoids the problem of having the attribute style being present but empty.

// Would be 0 if no styles are applied and > 0 if there are inline styles applied
contentWrapper.style.length
// So you can check for it like this
contentWrapper.style.length === 0

Comments

1

I missed @plalx's comment the first time I scanned this page.

if (element.hasAttribute("style"))
{
    var styleText = element.getAttribute("style")
}

On a related note, regarding styles...

//to get info about the end results of CSS
var computedStyle = element.currentStyle || getComputedStyle(element, null);

and

//to iterate over css styles from style tags or linked CSS
for i ...
    document.styleSheets[i].rules ...

//great for searching with
var elements = document.querySelectorAll(rules[i].selectorText);

Comments

1

To check style attribute exist or not for given Id

if(document.getElementById("idname").hasAttribute("style")){
  alert("Style attribute found");
}else{
  alert("Style attribute not found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.