toggling getElementByClassName problem

Discussion in 'JavaScript' started by gilgalbiblewheel, Jan 22, 2008.

  1. #1
    Where am I going wrong in this?
    //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="[-]";
       }
    }
    Code (markup):
    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;'>[b]</span> :<span class='forForum' style='display:none; font-weight: bold;'>[/b]</span><br />...
    Code (markup):

     
    gilgalbiblewheel, Jan 22, 2008 IP
  2. webwurks

    webwurks Well-Known Member

    Messages:
    126
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Can you send me the code you are calling this function with? Or the link to the page giving you this problem.
    It appears that the value you are pass the function is NOT an object that is known.
    It is possible that you are calling the function before the page is completely rendered?
     
    webwurks, Jan 25, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    Your problem is that your getElementsByClassName is returning an array & you're working with it as if it were an object/HTMLElement.

       obj=document.getElementsByClassName(item);
       obj = obj[0];
       visible=(obj.style.display!="none") //Line: 151
    Code (markup):
     
    joebert, Jan 25, 2008 IP