Probably every Javascript DOM programmer has tried to do this and realized it isn't a built-in function. As a bonus, it returns an array with all elements in the order they appear on the page. So here it is, with all the bells and whistles you could want:
<script> var elements = widget.getElementsByClassNames(classes,object,tags); </script>
classes is a comma-delimited list of all classes, such as "Class1,Class2,Class3"
object is the element inside which the search is conducted. Default is document
tags is a comma-delimited list of tags to include in the search, such as "div,span,p". Default is "*" (all tags).
For example, this script would hide all the <toc>'s in a page based on the fact that they are all of class "toc":
<script>
function hideAllTocs() {
var tocs = widget.getElementsByClassNames('toc',document.body,'div');
for (var i=0; i<tocs.length; i++) {
tocs[i].style.display = 'none';
}
}
widget.onDocLoad(hideAllTocs); //make sure entire DOM is loaded
</script>
Here, the function is called through onDocLoad so that the document is loaded before anything else happens.
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |