How will create Unordered List dynamically using JavaScripe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranyeng
    New Member
    • Feb 2008
    • 3

    How will create Unordered List dynamically using JavaScripe

    Suppose I have an Unordered List in my HTML file:


    [HTML]<html>
    <head>
    <SCRIPT LANGUAGE="JavaS cript">
    function remove(){
    //code to remove the old elements
    var d=document.getE lementById("for um");
    d.parentNode.re moveChild(d);
    //code to add new elements
    var li =document.creat eElement("li");
    var p = document.create TextNode('Hello ');
    li.appendChild( p);
    d.insertBefore( li, 1); //this is not working
    }
    </SCRIPT>
    </head>
    .....
    <ul id="forum">
    <li>JavaScrip t</li>
    <li>Forum</li>
    </ul>

    ....
    <input type="button" onclick="remove ()" >
    </html>[/HTML]


    Now, what is the code I have to write to remove to add new elements after removing all the parent elements?

    Plz sort out my problem... Thanks in advance.

    regards,
    Ranjan Y
    Last edited by gits; Mar 5 '08, 09:05 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    you could use the replaceChild() method for that purpose ...

    kind regards

    Comment

    • ranyeng
      New Member
      • Feb 2008
      • 3

      #3
      Originally posted by gits
      you could use the replaceChild() method for that purpose ...

      kind regards

      Hi Gits,
      Thanks for ur quick reply. What I exactly want is...

      Step 1: Remove all the existing elements of the unordered list
      Step 2: Start adding new elements

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        as i said ... you just could create a new ul- and its li-nodes and replace the current one or you shouldn't use removeChild fo the ul-node and set its innerHTML to an empty string instead and append the li-elements you create directly to the existing ul-node ...

        kind regards

        Comment

        • ranyeng
          New Member
          • Feb 2008
          • 3

          #5
          Originally posted by gits
          as i said ... you just could create a new ul- and its li-nodes and replace the current one or you shouldn't use removeChild fo the ul-node and set its innerHTML to an empty string instead and append the li-elements you create directly to the existing ul-node ...

          kind regards
          Thanks Gits... I have got the idea and my problem has been addressed.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            post back when you encounter problems with it :)

            kind regards

            Comment

            Working...