Results 1 to 1 of 1

Thread: Cannot read property of 'firstChild' error

  1. #1
    Join Date
    Feb 2012
    Location
    Ohio
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Cannot read property of 'firstChild' error

    I keep getting this error message: Uncaught TypeError: Cannot read property 'firstChild' of undefined filterScene

    I am trying to hide portions of the scene when the select list is selected.

    Code:
    function addEvent(object, evName, fnName, cap) {
       if (object.attachEvent)
           object.attachEvent("on" + evName, fnName);
       else if (object.addEventListener)
           object.addEventListener(evName, fnName, cap);
    }
    
    
    function uniqueElemText(elemName) {
       elems = document.getElementsByTagName(elemName);
       elemsArray = new Array();
    
       for (var i=0; i<elems.length; i++) elemsArray[i]=elems[i].innerHTML;  
       elemsArray.sort();
       for (i=0; i<elemsArray.length-1; i++) {
          if (elemsArray[i]==elemsArray[i+1]) {
             elemsArray.splice(i+1,1);
             i--;
          }
       }
       return elemsArray;
    }
    
    addEvent(window, "load", addCharList, false);
    
    var sourceDoc; // document on which the character list is based
    
    function addCharList() {
     newP = document.createElement("p");
     newP.innerHTML = "Show Only Lines By:";
     document.getElementById("characterList").appendChild(newP);
    
     selectList = document.createElement("select");
     selectList.id="cList";
     selectList.onchange=filterScene;
    
     newOption = document.createElement("option");
     newOption.innerHTML="Show All Character Lines";
     selectList.appendChild(newOption);
    
     var characters = uniqueElemText("h3");
     for (i=0; i<characters.length; i++) {
     newOption = document.createElement("option");
     newOption.innerHTML=characters[i];
     selectList.appendChild(newOption);
     }
    
     document.getElementById("characterList").appendChild(selectList);
    }
    
    function filterScene() {
     var displayStatus = "";
     for (var n = sourceDoc.firstChild; n != null; n = n.nextSibling) {
     var optionEntry = document.getElementById("cList");
     if (isHidden(optionEntry)) displayStatus = "none"
     else displayStatus = "";
    
     if (n.nodeType == 1) { // node represents a page element
     // apply the current display status to the node
     n.style.display = displayStatus;
     }
    
     }
    }
    
    function isHidden(object) {
     for (var n = object; n.nodeName != "BODY"; n = n.parentNode) {
     if (n.style.display == "none") return true;
     }
     
    return false;
    }
    Any help would be greatly appreciated!
    Last edited by championcyclones; 05-02-2012 at 08:10 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •