Jump to content

Recommended Posts

Just trying to right a simple script that will search for and display the child nodes and parent node when the applicable button is clicked. When clicking on "Show Child Nodes", nothing happens. When clicking on "Show Parent Node", the alert box just displays "null".

 

http://goken68.brinkster.net/ChildandParentNodes.html

 


<html>
<body>
<head>
<script type="text/javascript" language="javascript">

function getchildNodes() {

var content = document.getElementById("teams");//find the child Nodes of this element

for (var i = 0; i < content.length; i++)

{

  alert(content[i].childNodes.nodeValue);//display all the child nodes for the element, "teams".12:15 PM 2/8/2012

}
}

function getParentNode() {

var node = document.getElementById("north");//find parent node of this element.
alert(node.parentNode.nodeValue);//Display the parent node for element, "north".

}

</script>

</head>

<div id="teams">
<h1>NFL Teams</h1>

<h2 id="north">NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>

<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>

<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>

<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>

<input type="button" value="Show Child Nodes" onclick="getchildNodes()">
<br />
<br />
<input type="button" value="Show Parent Node" onclick="getParentNode()">



</body>
</html>



Link to comment
https://forums.phpfreaks.com/topic/256684-displaying-child-and-parent-nodes/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.