SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code example from book - getElementsById

    Hey. I'm reading "Simply Javascript", and I've been trying to code an example using the getElementsById method.

    First, the HTML in the body:
    Code:
    <p id="uniqueElement">Here's a link to the <a id="dogs" href="http://www.hsus.org/">Humane Society of the United States</a> for you.</p>
    Then the CSS:
    Code:
    #uniqueElement{
    	font-size: 24px;
    }
    And the script (verbatim, I believe) from the book:
    Code:
    var target = document.getElementById("uniqueElement");
    
    alert(target.nodeName);
    The script and style are linked from separate files. The content appears formatted in my browser (Firefox 2), but I get no alert... period. Any help?

    NOTE: I tried deleting the code and simply placing in
    Code:
    alert("Why are there problems!?!?!");
    The alert worked. There must not be a problem with linking the file from the HTML doc. I'm now having the same problem with the getElementsByTagName property.

  2. #2
    SitePoint Enthusiast BurakUeda's Avatar
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're using FireFox, you can open error console and see what's going on.

    And be sure you're getting the element with getElementById, not getElementsById.
    H u m o
    Uncensored Forums for Intelligent People

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Where is the code called from? If you try to access the element before it loads then it can't get the information.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's probably it. I suspect this code is above the </head> tag but is not contained within a window.onload function block.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I suspected something like that, as they warn about manipulating HTML that has not yet loaded. Now, they mention that there is a script in the "Core" library. How can I run that script first? Do I need to place the entire core before the script I want to run?

    Also, how can I use onLoad? Is that easier?

    Thanks for the help, guys.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <head>
    ...
    <script type="text/javascript">
    window.onload = function () {
      // put all your javascript that is to run when the window has completed loading HERE
      alert("window has loaded");
    }
    
    </script>
    ...
    </head>


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
  •