Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    failure getElementById?

    Please, how do i get this to work?

    <code>
    <header>
    <img id="imgpage" src='images/Mlogo.png' alt="mlogo">

    <script>
    var Pnam=document.getElementById("imgpage").src;
    document.writeln(Pnam+"|<BR>");
    // Following does not work
    document.getElementById("Ximgpage").src = "file:///D:/Test/images/O4Llogo.png";
    </script>


    <script>
    // Following does not work
    var Xnam=document.getElementById("Ximgpage").src;
    document.writeln(Xnam+"|<BR>");
    </script>

    <!-- Following works -->
    <img id="Ximgpage" src='images/ZlogoT.gif' alt="Tlogo">

    </header>
    </code>

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    It's not very easy to use document.getElementById without document !

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You can't use document.getElementById and document.writeln together.

    getElementById must run after the HTML has loaded while writeln must run before or it will overwrite the entire page (which is why writeln went out of use with Netscape 4)


    To use getElementById (and just about all other modern JavaScript) the JavaScript needs to come after all the HTML.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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