1

I'm having a couple issues where I need to get the styles of an element with native JS (Not JQ). However, using something like elem.style.property returns "" because the actual HTML element doesn't have it set on it.

To make things more specific, one thing I need to get is the display value of an element, but elem.style.display gives "", but I need to know "block" or "none"

I understand WHY that's happening; I just need to know the proper way to get that value with native JS.

Thanks.

asked Nov 20, 2013 at 12:57

1 Answer 1

1

Unless you have set the style directly on the element itself within your HTML you need to use either currentStyle or getComputedStyle e.g:

function getStyle(el,styleProp)
{
 if (el.currentStyle)
 return el.currentStyle[styleProp];
 return document.defaultView.getComputedStyle(el,null)[styleProp];
}

So for your purposes, you can call:

getStyle(element, 'visibility');
answered Nov 20, 2013 at 12:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.