Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append an element between two elements

I have a List

     <ul>
           <li id="lst1"> list item One</li>
           <li id="lst2"> list item two</li>
           <li id="lst3"> list item three</li>
           <li id="lst4"> list item four</li>
           <li id="lst5"> list item five</li>
     </ul>

and i want to append an item between (not inside) "lst2" and "lst3" i don't know how using jquery. Thanks.

like image 960
A. Rehman Javed Avatar asked Dec 18 '11 16:12

A. Rehman Javed


People also ask

How do you add an element after another element?

First, select the ul element by its id ( menu ) using the getElementById() method. Second, create a new list item using the createElement() method. Third, use the insertAfter () method to insert a list item element after the last list item element.

How do you add between in Java?

Use List<E>#add(int, E) to specify the position at which an element will be added to a list. Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).


2 Answers

$('#lst3').before('<li id="lst21"> list item three</li>');

You may try this.

like image 106
Sameera Thilakasiri Avatar answered Oct 14 '22 04:10

Sameera Thilakasiri


You can use afterdoc or insertAfterdoc:

$('#lst2').after('a');
$('a').insertAfter('#lst2');
like image 32
JMax Avatar answered Oct 14 '22 04:10

JMax