Jump to content

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>

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.