Forum Moderators: open
"case 'nextSibling': if (currentElement.nextSibling)
currentElement = currentElement.nextSibling;"
On the first of these lines it appears that one is meeting the case by saying the string value nextsibling has been passed to the function this line is contained in as the parameter named direction. This line also uses nextsibling is a property of the currentElement parameter as such the nextsibling property is a bolean. Then in the next line in quotation marks above we are updating the currentElement parameter in a way which requires it to be a string value. So is it a string a bolean value or is the nature of switch statements that you can meet a case equals string condition by including this condition in the if portion of this case statement as a string value? And this is a deviation from the normal way that and if condition requires something which evaluates to a bolean value?
Incidentally what this program does is traversed the DOM document tree. To operate this program and invoked the case I am talking about first click on the button marked "first child" and then click on the button marked "next sibling". They should invoke the case I am talking about with the word "body" outputted to the text window marked "node name" and the number 1 in outputted to the text window marked "node type"
So please tell me what you think about my question I am anxious to learn as much as I can about JavaScript.
Very sincerely
Marc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DOM Test</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<h1>DOM Test Heading</h1>
<hr />
<!-- Just a comment -->
<p>A paragraph of <em>text</em> is just an example</p>
<ul>
<li><a href="http://www.yahoo.com">Yahoo!</a></li>
</ul><form name="testform" id="testform" action="#" method="get">
Node Name: <input type="text" id="nodeName" name="nodeName" /><br />
Node Type: <input type="text" id="nodeType" name="nodeType" Value="#*$!" /><br />
Node Value: <input type= "text" id="nodeValue" name="nodeValue" Value="#*$!" /><br />
</form>
<script type="text/javascript">
<!--
function update(currentElement)
{
window.document.testform.nodeName.value = currentElement.nodeName;
window.document.testform.nodeType.value = currentElement.nodeType;
window.document.testform.nodeValue.value = currentElement.nodeValue;
}
function nodeMove(currentElement, direction)
{
switch (direction)
{
case "previousSibling": if (currentElement.previousSibling)
currentElement = currentElement.previousSibling;
else
alert("No previous sibling");
break;
[COLOR=DarkOrange]case "nextSibling": if (currentElement.nextSibling)
currentElement = currentElement.nextSibling; [/COLOR]
else
alert("No next sibling");
break;
case "parent": if (currentElement.parentNode)
currentElement = currentElement.parentNode;
else
alert("No parent");
break;
case "firstChild": if (currentElement.hasChildNodes())
currentElement = currentElement.firstChild;
else
alert("No Children");
break;
case "lastChild": if (currentElement.hasChildNodes())
currentElement = currentElement.lastChild;
else
alert("No Children");
break;
default: alert("Bad direction call");
}
update(currentElement);
return currentElement;
}
var currentElement = document.documentElement;
update(currentElement);
//-->
</script>
<form action="#" method="get">
<input type="button" value="Parent"
onclick="currentElement=nodeMove(currentElement,'parent');" />
<input type="button" value="First Child"
onclick="currentElement=nodeMove(currentElement,'firstChild');" />
<input type="button" value="Last Child"
onclick="currentElement=nodeMove(currentElement,'lastChild');" />
<input type="button" value="Next Sibling"
onclick="currentElement=nodeMove(currentElement,'nextSibling');" />
<input type="button" value="Previous Sibling"
onclick="currentElement=nodeMove(currentElement,'previousSibling');" />
<input type="button" value="Reset to Root"
onclick="currentElement=document.documentElement; update(currentElement);" />
</form>
</body>
</html>
//test an empty string
if(""){document.write("true");}
else{document.write("false");}
document.write("<br>");//test a string with a value
if("string"){document.write("true");}
else{document.write("false");}
document.write("<br>");//test an empty array
if(new Array()){document.write("true");}
else{document.write("false");}
document.write("<br>");//test an array with values that are null
if(new Array("")){document.write("true");}
else{document.write("false");}
document.write("<br>");//test an empty value of an array
if(new Array("")[0]){document.write("true");}
else{document.write("false");}
document.write("<br>");//test an array with values
if(new Array("string")){document.write("true");}
else{document.write("false");}
document.write("<br>");
false
true
true
true
false
true
so when if(element.nextSibling) is written it is testing whether or not that element has a nextSibling property set.