Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add li to middle of the ul (jquery)

How one can add li to middle of the ul.

For example if i have list like this:

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>

And i want to add addiion li (<li>new</li>) after 3rd list?

like image 939
mkoso Avatar asked Feb 15 '10 21:02

mkoso


People also ask

How to add new li in ul using jQuery?

Answer: Use the jQuery append() Method You can simply use the jQuery append() method to add <li> elements in an existing <ul> element. The following example will add a <li> element at the end of an <ul> on click of the button.

How to add element in list using jQuery?

Using append() method: The append() method in jQuery is used to add a new element at the end of the selected element. Parameter: This method accepts single parameter element which need to be inserted.

Which method would you use to add one new item Li item in the beginning of ordered list?

With jQuery, you can create a new <li> element and add it to an existing <ol> or <ul> element using the . append() or the . prepend() method.


1 Answers

var middle = Math.ceil($("#myUL li").length / 2);
$("#myUL li:nth-child(" + middle + ")").after('<li>Item New</li>');
like image 89
Francisco Aquino Avatar answered Sep 20 '22 15:09

Francisco Aquino