SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display HTML in Javascript

    I'm trying to display the elements in an array but doesn't seem to work:

    Code:
    var myurl=new Array("google.com", "yahoo.com"); 
    
    for (i=0;i<=myurl.length-1;i++){
       document.writeln("<a href='http://www.' +  myurl[i] +  target='_blank'>");
    }

    Not sure why it is not working??

  2. #2
    SitePoint Enthusiast aufshebung's Avatar
    Join Date
    May 2012
    Location
    Santa Fe, NM
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The immediate problem is that your code has some messed up quote marks. It should look like this, instead:

    Code javascript:
    document.writeln("<a href='http://www." + myurl[i] + "' target='_blank'>");

    The next issue is that the links you're creating don't have any content in them. Here's what the HTML will look like once it's generated:

    HTML Code:
    <a href='http://www.google.com' target='_blank'>
    <a href='http://www.yahoo.com' target='_blank'>
    What text do you want to use for the content of the link? The easiest solution is to just use the domain. To do that, change your JS to this:

    Code javascript:
    document.writeln("<a href='http://www." + myurl[i] + "' target='_blank'>" + myurl[i] + "</a>");

    That should give you HTML like this:

    HTML Code:
    <a href='http://www.google.com' target='_blank'>google.com</a>
    <a href='http://www.yahoo.com' target='_blank'>yahoo.com</a>
    Another possible snafu is that, depending on when/how you use "document.writeln", the rest of the content that already exists may be thrown away. (I'm not so sure about this, though. I might be making it up...)

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use jquery templates. You simply write one-line template (the <a href...> markup) and then you call the template multiple times each with its own parameters


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
  •