Web Analytics Made Easy -
StatCounter Adding UL element to Li - CodingForum

Announcement

Collapse
No announcement yet.

Adding UL element to Li

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Show the code for your existing <ul><li>... HTML.

    How will you choose WHICH <li> to add the new <ul> to?
    Be yourself. No one else is as qualified.

    Comment


    • #3
      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);

      Comment


      • #4
        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);
        Be yourself. No one else is as qualified.

        Comment

        Working...
        X