Wolverine68 Posted February 4, 2012 Share Posted February 4, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/256408-trying-to-display-nodes-using-getelementsbytagname/ Share on other sites More sharing options...
nogray Posted February 5, 2012 Share Posted February 5, 2012 getElementsByTagName needs a tag as a string, you can use document.childNodes() to get all the child elements. You will have to check for nodeType since this will also return text nodes. Quote Link to comment https://forums.phpfreaks.com/topic/256408-trying-to-display-nodes-using-getelementsbytagname/#findComment-1314675 Share on other sites More sharing options...
Wolverine68 Posted February 7, 2012 Author Share Posted February 7, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/256408-trying-to-display-nodes-using-getelementsbytagname/#findComment-1315308 Share on other sites More sharing options...
nogray Posted February 8, 2012 Share Posted February 8, 2012 Change alert(content[i].childNodes.innerHTML); to alert(content[i].innerHTML); Quote Link to comment https://forums.phpfreaks.com/topic/256408-trying-to-display-nodes-using-getelementsbytagname/#findComment-1315679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.