...

View Full Version : Adding UL element to Li



rajesh panduru
08-19-2011, 08: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, 09: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, 06: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, 09: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);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum