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>
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) {
var n = document.getElementsByClassName(className);
for (var i = n.length-1; i>=0;i--) n[i].style.display = 'block';
}
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)
{
var divs = document.getElementsByTagName("div");
for (var d - 0; d < divs.length; ++d )
{
var div = divs[d];
if ( div.className == className )
{
div.style.display = show ? "block" : "none";
}
}
return false;
}
...
<a href-"#" onclick="return showHideAll('show',true);"> Show all </a>
<a href-"#" onclick="return showHideAll('show',false);"> Hide all </a>
You could create a replacement for getElementsByClassName, but if this would be the only use for it on the page, there's not much point.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 01-16-2013 at 10:21 PM..
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.
You might do it this way if you had multiple "holder" divs where you wanted this kind of control and/or if your <body> needed to be able to change class names for other reasons.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
It only fails if you use hyphens in your class names. While - and _ are technically allowed they are not compatible with all browsers. In particular using - in a class can break a lot of JavaScript references to it since it could possibly end up being treated as a minus sign and definitely gets treated as a word boundary.
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.
It only fails if you use hyphens in your class names.
Anyway if you insist on using hyphens in class names then you could replace the '\\b'+cl+'\\b' in the regexp with '(^| )'+cl+'( |$)' ....
my point was that if good coders like you can make mistakes, what else is missing, how much can we rely on such a function, and do we really NEED to hinge our entire operation on such a contraption.
there are other conformance and performance issues with the script, i'm not going to nit pick.
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
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%