How to insert new item in middle of unordered list

M

michael

Good Morning,


I have an unordered list similar to:

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

I want to be able to insert a new list item anywhere in the list, for
example, between Item 1 and Item2.

How can this be done with Javascript and DOM?


Cheers!
 
J

Jonas Raoni

michael escreveu:
Good Morning,

Good night :D
I have an unordered list similar to:
<ul>
<li>Item 1</li>
:
</ul>

I want to be able to insert a new list item anywhere in the list, for
example, between Item 1 and Item2.

ULElement.inserBefore(newLI, referenceLI);

ex:
var ul = document.getElementById("myList"), li =
document.createElement("li");
ul.insertBefore(li, ul.getElementsByTagName("li")[1]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top