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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding UL element to Li

    hi All,

    I want to add UL item to Li in java script dynamically .so that i can build submenu dynamically.can any one help me.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Show the code for your existing <ul><li>... HTML.

    How will you choose WHICH <li> to add the new <ul> to?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i will identify the li tag by id . and want to add UL item to that li tag in javascript
    sample code

    in the below sample code i am adding new li tag to existing UL tag and i am adding <a> to li item and i wanted to add <UL> tag also to li .

    var container = document.getElementById('sub3'); // existing UL tag
    // Create a new <li> element for to insert inside <ul id="myList">
    var new_element = document.createElement('li');
    var a = document.createElement('a');


    a.setAttribute('href', "#")

    a.appendChild(document.createTextNode(favName));

    container.insertBefore(new_element, container.firstChild);
    new_element.appendChild(a);

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Code:
        var container = document.getElementById('sub3'); // existing UL tag
        // Create a new <li> element for to insert inside <ul id="myList">
        var new_element = document.createElement('li');
        var a = document.createElement('a');
        a.setAttribute('href', "#")
        a.appendChild(document.createTextNode(favName));
        new_element.appendChild(a);   // moved this line
    
        var ul = document.createElement("ul");
        new_element.appendChild(ul);
        
        container.insertBefore(new_element, container.firstChild);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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