Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2005, 12:55 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help accessing specific nodes

I'm currently reading the "[u]Professional JavaScript for Web Developers</u>" book by Nicholas C Zakas. In page 171 he talks about how to access specific nodes (he is talking about the DOM) and he shows a way on how to get to them using the tag name. Anyway, this is the code that I'm having trouble with:

Code:
<html>
    <head>
        <title>DOM Example</title>
        <script>
        //<![CDATA[
        var oPs = document.getElementsByTagName("p");
        var oImgsInP = oPs[0].getElementsByTagName("img");
        //]]>
        </script>
    </head>
    <body>
        <p style="color:red" id="p1">
        <img src="images/donramon.jpg" name="something" />

        </p>
        <input type="button" value="show the tag" />
    </body>
</html>
I keep getting the "oPs.1 is null or not an object" and I am copying the code exactly as it is in the book, can anyone help please?
 
Old June 29th, 2005, 04:49 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

The error is occurring because the script is running before the page is built & the document content therefore is not available.
Try changing the script to...
Code:
window.onload = function(){
    var oPs = document.getElementsByTagName("p");
    var oImgsInP = oPs[0].getElementsByTagName("img");
}
HTH,

Chris

 
Old June 29th, 2005, 09:45 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I didn't ever think about that... I'm a JavaScript beginner trying to become a JavaScript Expert so that might have something to do with the problem.:D





Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing specific Nodes Krippers XSLT 3 November 6th, 2008 05:38 AM
To Select a specific nodes from XML vkommera XSLT 4 June 27th, 2008 03:39 PM
Accessing nodes contained in a variable geoGregory3 XSLT 2 March 12th, 2008 02:57 PM
Accessing higher level nodes from a template mikeymikey XSLT 3 January 3rd, 2008 09:14 PM
problem accessing nodes using document() and id() siris phi XSLT 3 April 12th, 2007 11:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.