SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    FBI secret agent digitman's Avatar
    Join Date
    Sep 2004
    Location
    Work
    Posts
    697
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Stupid getElementById()

    I've been trying to follow the example from the sitepoint book on DHTML to change the href property of a link, but it doesn't work no matter what I try. Here's the code in the HTML file:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//
        "
    http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <
    head>
    <
    titleJavascript testing </title>
    <
    script type="text/javascript" src="js/main.js"></script>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <a href="http://www.yahoo.com" id="changeMe">Yahoo </a>
    </body>
    </html> 
    Here's the code in 'js/main.js' file:
    PHP Code:
    changeLink();
    function 
    changeLink()
    {
    var 
    linkToChange document.getElementById('changeMe')
    linkToChange.href="http://www.google.com";
    alert('done!');
    return 
    true;

    Whenever I try to run this, the mozilla console gives this error:
    Quote Originally Posted by mozilla
    Error: linkToChange has no properties
    Source File: file:///E:/www/js/js/main.js
    Line: 5
    What am I doing wrong??

    Edit: Nevermind, I just fixed this problem by moving the line that calls up main.js at the end of all the html and it worked.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by digitman
    Edit: Nevermind, I just fixed this problem by moving the line that calls up main.js at the end of all the html and it worked.
    You can keep it up top or in a separate file if you have your function executed when window.onLoad goes off. The page will be loaded by then so your link will exist.


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
  •