rajesh panduru
08-19-2011, 07:15 PM
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.
Old Pedant
08-19-2011, 08:14 PM
Show the code for your existing <ul><li>... HTML.
How will you choose WHICH <li> to add the new <ul> to?
rajesh panduru
08-20-2011, 05:59 AM
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);
Old Pedant
08-20-2011, 08:33 PM
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);