![]() |
getElementsByClassName to call multiple classes of same name
I have several sections of content on my site that are default 'hidden' in CSS. I use the getElementById so visitors can open each section of content individually. I now would like to add the option to open ALL content at once. All sections are contained in <div> tags with the class="show". How could I do this with getElementsByClassName?
Here's what I have so far: function showHideAll(className) { document.getElementsByClassName(className).style.display = 'block'; } <a href="#" id="showAll" class="showLink" onclick="showAll('show');return false;">SHOW ALL CONTENT</a> <div id="content" class="show">CONTENT 1</div> <div id="content2" class="show">CONTENT 2</div> <div id="content3" class="show">CONTENT 3</div> EXAMPLE PAGE: http://carevealed.com/cc.php |
getElementsByClassName returns nodelist and not an individual node so what you need is a loop that processes the nodelist to set the style.display to 'block' for each entry in the nodelist.
Code:
function showHideAll(className) { |
And we should note that getElementsByClassName is not supported by older MSIE browsers.
So you might want to just stick with getElementsByTagName. Code:
function showHideAllDivs(className, show) |
it would be better to avoid compat problems by simply creating a CSS rule that hides all the classes you want to hide: no loops, no compat issues, and faster performance.
Code:
<style>Code:
document.body.className="hide"; // to hide |
Good one, RndMe.
You could also do it a bit more restrictive, if that mattered: Code:
<style type="text/css"> |
To cater for old IE simply include the following additional code to define getElementsByClassName for when the browser doesn't:
Code:
if (!document.getElementsByClassName) |
Quote:
this is exactly why i recommended using CSS: Code:
if (!document.getElementsByClassName2) document.getElementsByClassName2 = function(cl) { |
Well, we could work harder on that regular expression and correct the problem, but yeah, your CSS trick works a lot better.
FWIW, in my replacement for getElementsByClassName I simply look for the given string to be contained in the className. Yes, I know that means that looking for "one" will incorrectly find "one-active" or even "none" or "lonesome". But that just means I'm really careful about making sure my class names don't have potential problems like that. |
Quote:
Anyway if you insist on using hyphens in class names then you could replace the '\\b'+cl+'\\b' in the regexp with '(^| )'+cl+'( |$)' so that it specifically tests just for space and start/end string boundaries. Then your classes would only malfunction in the other places where JavaScript treats the hyphen differently. |
Quote:
there are other conformance and performance issues with the script, i'm not going to nit pick. We collectively built a decent one a while back: http://www.codingforums.com/showthread.php?t=154727 (2009) even still, i think that avoiding loops is always good, avoiding hidden snags is always good, and that CSS offers more precise group definition since its CSS selectors work in IE7 without a bunch of bring-your-own code. cheers |
| All times are GMT +1. The time now is 06:04 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.