SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast sarmenhb's Avatar
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why wont this piece of code work?

    I'm reading sitepoints simply javascript book and this piece of code from page 92-93 wont work..

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    
    var target = document.getElementById("berenger");
    alert(target.nodeName);
    
    </script>
    
    
    </head>
    <body>
    
    
    <h1>
    Sniper (1993)
    </h1>
    <p>
    In this cinema masterpiece,
    <a id="berenger" href="/name/nm0000297/">Tom Berenger</a> plays
    a US soldier working in the Panamanian jungle.
    </p>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sarmenhb View Post
    I'm reading sitepoints simply javascript book and this piece of code from page 92-93 wont work..
    Snip.

    Does it specify to place the code in the <head> section? I hope not. It needs to be positioned below the element that it addresses.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Enthusiast sarmenhb's Avatar
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you dont mind, can you write this out another way so that i can make it work and learn from it.. thanks

  4. #4
    SitePoint Enthusiast sarmenhb's Avatar
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, i figured it out. i just said to myself let me try something and it worked.
    this is what i did


    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function test() { 
    var target = document.getElementById("berenger");
    alert(target.nodeName);
    }
    </script>
    
    
    </head>
    <body onload="test()">
    
    
    <h1>
    Sniper (1993)
    </h1>
    <p>
    In this cinema masterpiece,
    <a id="berenger" href="/name/nm0000297/">Tom Berenger</a> plays
    a US soldier working in the Panamanian jungle.
    </p>
    </body>
    </html>

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sarmenhb View Post
    lol, i figured it out.
    O.K. In my experience, text books by multiple authors are to be avoided.
    Tab-indentation is a crime against humanity.


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
  •