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 2 of 2
  1. #1
    don
    don is offline
    New Coder
    Join Date
    Sep 2002
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I dynamically add a list item?

    How do I add a list item to this list using JavaScript? I'd like to do it on the onLoad event to create a menu dynamically.

    <div id="navmain">
    <ul>
    <li><a href="./home.htm">Home</a></li>
    <li><a href="./sitemap.htm">Site Map</a></li>
    </ul>
    </div>

    Also, what's a good site that breaks down DOM and DHTML similar to http://www.blooberry.com/indexdot/index.html for HTML and CSS?

    Thanks,
    Don


    UPDATE:

    I figured out how to add the <li>, but I wanted to add it between the "Home" and "Site Map" nodes.

    For others, this is how I'm doing it now (not tidied up yet):

    function navMainItem(text,href) {
    a = document.createElement("a");
    a.href = href;
    a.appendChild(document.createTextNode(text));
    li = document.createElement("li");
    li.appendChild(a)
    ul = document.getElementById("navmainul");
    ul.appendChild(li);
    }
    Last edited by don; 01-20-2005 at 09:37 PM.

  • #2
    don
    don is offline
    New Coder
    Join Date
    Sep 2002
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind. I figured it out. Sorry for the blow by blow, folks.

    If anyone cares, I hard-coded the ID of the sitemap item and call the navMainLoad() routine from the onLoad event of the <body>. I'll make it more generic, but for now...

    function navMainItem(text,href) {
    a = document.createElement("a");
    a.href = href;
    a.appendChild(document.createTextNode(text));
    li = document.createElement("li");
    li.appendChild(a)
    ul = document.getElementById("navmainul");
    ul.insertBefore(li, document.getElementById("navmainsitemap"))
    }

    function navMainLoad () {
    navMainItem ("APPLE", "http://www.apple.com")
    navMainItem ("GOOGLE", "http://www.google.com")
    navMainItem ("APPLE", "http://www.apple.com")
    navMainItem ("GOOGLE", "http://www.google.com")
    navMainItem ("APPLE", "http://www.apple.com")
    navMainItem ("GOOGLE", "http://www.google.com")
    navMainItem ("APPLE", "http://www.apple.com")
    navMainItem ("GOOGLE", "http://www.google.com")
    }


  •  

    Posting Permissions

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