How to show/hide a class

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
aka.snowball
Forum Newbie
Posts: 5
Joined: Tue Jun 22, 2010 6:38 am

How to show/hide a class

Post 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 :)
aka.snowball
Forum Newbie
Posts: 5
Joined: Tue Jun 22, 2010 6:38 am

Re: How to show/hide a class

Post 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. :)
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

currentStyle and getComputedStyle

Post 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.
Post Reply