SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question pp. 68-69 of "Simply JavaScript"

    I am working my way through Sitepoint's book "Simply JavaScript", but I cannot get the code on pp. 68-69 to work. The alert box will not appear. If I try this:
    alert(target);
    the box appears and the result is:
    null

    Any tips on what I am doing wrong?

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The variable called target appears to be empty.
    Check carefully the previous statements that involve the target variable.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The variable called target appears to be empty.
    Check carefully the previous statements that involve the target variable.


    Here is the code from the book:

    var target = document.getElementById("berenger");
    alert(target.nodeName);


    I have this in the HTML page:

    <h1 id="berenger">Sniper (1993)</h1>

    When I load the HMTL page, nothing happens. If I change the second line in the .js file to this:

    alert(target);

    then the alert box pops up with "null" as the response. So, for some reason the first part of the script is not retrieving the "h1" tag element.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You'll get that if the script is running before the page element is available.

    Look at page 58-59 about unobtrusive scripting in the real world, which takes you through how to run the code after the page has loaded.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •