PDA

View Full Version : Resolved Cannot read property of 'firstChild' error



championcyclones
04-23-2012, 11:39 PM
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.



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!