Jump to content

Trying to display nodes using getElementsByTagName


Wolverine68

Recommended Posts

Trying to run a script that will display the name of every node in an HTML document. The URL to my page is below. When clicking on the "Show nodes" button, it gives "object expected" error for line 50

 

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

 


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

function getNodes() {

var content = document.getElementsByTagName(*);

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

{

  document.write(content[i].nodeName.innerHTML);

}
}

</script>

</head>


<h1>NFL Teams</h1>

<h2>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>

<input type="button" value="Show Nodes" onclick="getNodes()">



</body>
</html>


 

 

Made some changes. Now, the alert box only displays "undefined".

 

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

 


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

function getNodes() {

var content = document.getElementsByTagName('*');//using asterick as a wild card so that all nodes will be searched.

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

{

  alert(content[i].childNodes.innerHTML);

}
}

</script>

</head>

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

<h2>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 Nodes" onclick="getNodes()">



</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.