Page 1 of 1

How to show/hide a class

Posted: Wed Jun 23, 2010 5:46 am
by aka.snowball
Hi I have a javascript function, that checks if a class is visible or if its hidden. Problem is that this dosn't work.


this works

Code: Select all

allPageTags[i].style.display = 'none';
but this don't

Code: Select all

if (allPageTags[i].style.display ==''){
	allPageTags[i].style.display = 'none';
}
else{
allPageTags[i].style.display = '';
}
in other words allPageTags.style.display is either 'none' or ''. This works to check if an id is visible, but how do i check if a class is visible, my atempt didn't work obviously.

So anyone know how to check if a current class is visible or if its "visible=='none"'??

Thanks :)

Re: How to show/hide a class

Posted: Wed Jun 23, 2010 5:56 am
by aka.snowball

Code: Select all

<th><a href="javascript:displayTestSuite('test');">   test2</a>
</th>
I want depending on if the class test is shown, hide that row that has that class, or show it if its hidden. :)

currentStyle and getComputedStyle

Posted: Wed Jun 23, 2010 1:54 pm
by tr0gd0rr
So you are saying that the element may have a class with `display: none`?

If so, you'll need to read the computed style using `currentStyle` (IE) and `getComputedStyle` (other browsers).

Check out the last post on this codingforums.com thread.The final bit of code looks ok, but untested:

Code: Select all

function isVisible(elem) {
   var cmpstyle = ('getComputedStyle' in window) ? window.getComputedStyle(elem,null) : elem.currentStyle;
   if (parseFloat(cmpstyle.opacity) > 0 &&
      cmpstyle.visibility != 'hidden' &&
      cmpstyle.display != 'none') {
      return true;
   }
   return false;
}
You'll notice that it also checks if the item is hidden or opacity 0 which may not be useful to you.