PDA

View Full Version : toggling getElementByClassName problem


gilgalbiblewhee
01-22-2008, 10:05 AM
Where am I going wrong in this?
Error: obj.style has no properties
Source File: showbook.php?keyword1=What%20have&keyword2=thou%20Son%20of%20God&keyword3=to%20do%20with%20thee&keyword4=swine&keyword5=devil&keyword6=torment
Line: 151
//http://javascript.about.com/library/bldom08.htm
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};

function Toggle(item) {
obj=document.getElementsByClassName(item);
visible=(obj.style.display!="none") //Line: 151
key=document.getElementById("x" + item);
if (visible) {
obj.style.display="none";
key.innerHTML="[+]";
} else {
obj.style.display="block";
key.innerHTML="[-]";
}
}

and
<div ID="support" style="display:none; margin-left:2em">
<span class='forForum' style='display:none; font-weight: bold;'>[quote]</span>
<span class='forForum' style='display:none; font-weight: bold;'></span> :<span class='forForum' style='display:none; font-weight: bold;'></span><br />...

Kor
01-22-2008, 11:12 AM
the getElementsByClassName() custom method returns a collection of elements (an array), not a single element...

gilgalbiblewhee
01-23-2008, 05:04 AM
This is getting complicated. It seems that I need to set an array of line 151. There's no use of having a classname. I think I'm going to sqitch it back to getElementById but how do I make it so that it will pick up all the ids of the span tags and apply it?

Kor
01-23-2008, 08:48 AM
I don't get it... Which is the event? (onclick?) and where it is applied? Which kind of elements you want to hide/show? Are they grouped somehow?