Jump to content

How can I check the display style?

Recommended Posts

I tried several times already and it still won't work. :)

<div id="con2" style="display:block;">Text</text><script type="text/javascript">if(document.getElementById('con2').style.display == "block") { alert("Ok");}</script>

EDIT: It works now. :); Apparently, external css doesn't work.

Edited by Cod-nes
Link to post
Share on other sites

Why does the closing tag read "text" and not "div"? Besides being invalid, that would cause the division element to remain open past the script tag, and thus not be ready when the script is run.

Link to post
Share on other sites

to read styling from external css, you need to use

<script type="text/javascript">if(getStyle(document.getElementById('con2'), 'display') == "block") {alert("Ok");}function getStyle(el, cssprop){ if (el.currentStyle) //IE  return el.currentStyle[cssprop] else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox  return document.defaultView.getComputedStyle(el, "")[cssprop] else //try and get inline style  return el.style[cssprop]}</script>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...