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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can't Figure Out - document.getElementById

    I'm trying convert document.write to document.getElementById
    but it is not working and I can't seem to figure it out.



    This is my original code:
    Code:
    var myurl = new Array("google.com", "yahoo.com"); 
                    
    for (i=0;i<=myurl.length-1;i++){
       document.write("<a href='http://www." +  myurl[i] + " target='_blank'>"+ myurl[i]+"</a>");
    }
    I would like to convert to document.getElementById but it doesn't work.

    Code:
    <HTML>
    <HEAD>
    <TITLE>Test Input</TITLE>
    <script type="text/javascript">
    
       var myurl = new Array("google.com", "yahoo.com"); 
    
       document.getElementById('info').innerHTML = "<table border=1><tr><td>";                
    
       for (i=0;i<=myurl.length-1;i++){
          document.getElementById('info').innerHTML += "<tr><td><a href='http://www." +  myurl[i] + " target='_blank'>"+ myurl[i]+"</a></tr></td>";
       }
       document.getElementById('info').innerHTML = "</tr></td></table>";
    </script>
    </HEAD>
    
    <BODY>
    
    
    <p id="info"></p>
    
    </BODY>
    </HTML>

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,774
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Code:
    <HTML>
    <HEAD>
    <TITLE>Test Input</TITLE>
    <script type="text/javascript">
    	var myurl = new Array("google.com", "yahoo.com"); 
    	var infoContent = "<table border=1><tr><td>";               
    	for (i=0; i<=myurl.length-1; i++)
    	{
    		infoContent += "<a href='http://www." +  myurl[i] + " target='_blank'>"+ myurl[i]+"</a>";
    	}
    	infoContent += "</tr></td></table>";
    	document.getElementById('info').innerHTML = infoContent;
    </script>
    </HEAD>
    <BODY>
    	<p id="info"></p>
    </BODY>
    </HTML>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    The script needs to go at the bottom just before the </body>

    The id has to exist before you can reference it.
    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.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    I don't think it is legal html to put a table inside a paragraph, is it?

    LT
    Last edited by low tech; 05-28-2012 at 08:57 AM. Reason: added missing word -- think
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by low tech View Post
    I don't it is legal html to put a table inside a paragraph, is it?

    LT
    Correct. You can't put a <table> inside a <p>.

    So you're trying to insert invalid HTML and the browser will change <p><table></table></p> into <p></p><table></table> (i.e. moving the table child up to a sibling) in order to get valid HTML.

    If you give the browser invalid HTML, the browser will guess what you really mean and go with its guess.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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