SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript create new elements

    Does any one know how to add new elements to a page in javascript? For example I have a for loop that has to be done 4 times. For each of the time i want to add a link tag to my page, so in the end I have for new links on my page based on the loop. How can this be done. In php I know I can just echo the enter link code with some ajax but I want to do this without php involvement.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.createElement
    document.createTextNode


    and then use appendChild to attach the elements to the other elements and the page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var new_link = document.createElement('a');
    new_link.href = 'http://www.google.com';
    new_link.innerHTML = 'Go to Google!';

    document.getElementById('destinationDiv').appendChild(new_link);


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
  •