- Mar 26, 2005
- 4,094
- 123
- 106
So I just got into the topic about DOM...
I am trying to build a function that would loop through all nodes in the document and if the selected node was found it will say so and if it was not found it will also say so.
Something in my code is not working, and I messed up on my if/else if cases somewhere because the not found message comes out regardless. Can someone please tell me what I am doing wrong? Maybe I am just tired, but I cannot see it...
I am trying to build a function that would loop through all nodes in the document and if the selected node was found it will say so and if it was not found it will also say so.
Something in my code is not working, and I messed up on my if/else if cases somewhere because the not found message comes out regardless. Can someone please tell me what I am doing wrong? Maybe I am just tired, but I cannot see it...
Code:
function CheckNodeType() {
var selection = parseInt(prompt("This program checks the webpage to see if the node type you entered exists.\n\nPlease enter:\n\n1 to check for Element\n2 to check for Attr\n3 to check for Text\n8 to check for Comment \n9 to check for Document"));
if (selection < 1 || selection > 9){
alert("Your selection was not what the program is asking for! Run the program again");
}
else if(selection >3 && selection <8){
alert("Your selection was not what the program is asking for! Run the program again");
}
else if(isNaN(selection)){
alert("Your selection was not what the program is asking for! Run the program again");
}
var childNodes = document.childNodes;
for(var i=0; i<childNodes.length; i++) {
if (childNodes[i].nodeType === 1 && selection === 1) {
alert("Yes, there is an Element node");
}
else if (childNodes[i].nodeType === 2 && selection === 2){
alert("Yes, there is an Attr node") ;
}
else if (childNodes[i].nodeType === 3 && selection === 3){
alert("Yes, there is a Text node") ;
}
else if (childNodes[i].nodeType === 8 && selection === 8){
alert("Yes, there is a Comment node") ;
}
else if (childNodes[i].nodeType === 9 && selection === 9){
alert("Yes, there is a Document node") ;
}
else {
alert("Node type was not found");
}
}
