www.webdeveloper.com
Results 1 to 3 of 3

Thread: document.getElementById - Learning Javascript

  1. #1
    Join Date
    Sep 2012
    Location
    Houston, TX
    Posts
    4

    document.getElementById - Learning Javascript

    Hello,

    I'm trying to learn Javascript and im obviosly having some difficulties with the document.getElementById to replace HTML. In the real page there is a function to determine the variable for streama based on the URL etc etc... but the function works perfectly so i have simplified it for the sake of this issue to just defining the variable. Despite all the tutorials and examples i can find I cant see to see the problem with the way i wrote this... the current output is the following...

    Watch live video from No Stream on www.twitch.tv
    theerebus

    As you can probly tell by the script the intene

    Watch live video from theerebus on www.twitch.tv
    theerebus

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <script>
    var streama = "theerebus";
    document.getElementById("stream1").href="http://www.twitch.tv/";
    document.getElementById("s").innerHTML="Watch live video from " + streama + " on www.twitch.tv";
    </script>
    
    <a href="http://www.twitch.tv/Nostream" class="trk" id="s" style="padding:2px 0px 4px; display:block; width:345px; font-weight:normal; font-size:10px; text-decoration:underline; text-align:center;">Watch live video from No Stream on www.twitch.tv</a>
    
    <script>
    document.write(streama);
    </script>
    
    </body>
    </html>

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Your Javascript is executing before the element you want it to modify is even loaded.

    Put your JS at the end of you HTML or make it a function ond do an onload event on the body.

  3. #3
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    98
    Two things:

    - i don't see the "stream1" ID declared in your example, remember to declare it in the HTML element envolved;

    - the correct JS syntax to handle 'href' is:
    document.getElementById('someID').location.href='somevalue'

    Hope that will help someway ;-)
    Last edited by MrSnowDrop; 09-19-2012 at 04:24 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles