I m trying to toggle the visibility of certain DIV elements on a website depending on the class of each DIV. I m using a basic JavaScript snippet to toggle them. The problem is that the script only uses getElementById
, as getElementByClass
is not supported in JavaScript. And unfortunately I do have to use class and not id to name the DIVs because the DIV names are dynamically generated by my XSLT stylesheet using certain category names.
I know that certain browsers now support getElementByClass
, but since Internet Explorer doesn t I don t want to go that route.
I ve found scripts using functions to get elements by class (such as #8 on this page: http://www.dustindiaz.com/top-ten-javascript/), but I can t figure out how to integrate them with with my toggle script.
Here s the HTML code. The DIVs themselves are missing since they are generated on page load with XML/XSLT.
Main Question: How do I get the below Toggle script to get Element by Class instead of get Element by ID?
<html>
<head>
<!--This is the TOGGLE script-->
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == block )
e.style.display = none ;
else
e.style.display = block ;
}
//-->
</script>
</head>
<!--the XML/XSLT page contents will be loaded here, with DIVs named by Class separating dozens of li s-->
<a href="#" onclick="toggle_visibility( class1 );">Click here to toggle visibility of class 1 objects</a>
<a href="#" onclick="toggle_visibility( class2 );">Click here to toggle visibility of class 2 objects</a>
</body>
</html>