tags:

views:

88

answers:

2

Hi

I wanted to insert a li tag in the middle of a list of li tags based on a css class set to the li tag using jQuery. Consider the following

<ul class="myList">
     <li><a href="#">test 1</a></li>
     <li class="active"><a href="#">test 2</a></li>
     <li><a href="#">test 3</a></li>
     <li><a href="#">test 4</a></li>
    <li><a href="#">test 5</a></li>
    <li><a href="#">test 6</a></li>
</ul>

I wanted to insert a new li tag after the li tag set to active. So the output will be like this.

<ul class="myList">
     <li><a href="#">test 1</a></li>
     <li class="active"><a href="#">test 2</a></li>
     <li><a href="#">My new Tag</a></li>
     <li><a href="#">test 3</a></li>
     <li><a href="#">test 4</a></li>
    <li><a href="#">test 5</a></li>
    <li><a href="#">test 6</a></li>
</ul>

I tried with .appendTo, .insertAfter, .append etc. but could not get the result I wanted. Any idea how this can be achieved?

+4  A: 
$('li.active').after('<li><a href="#">My new Tag</a></li>');
Matthew Flaschen
This works.. Thank you..!
Amit
A: 

Try this:

$('<li><a href="#">content here</a></li>').insertAfter('ul.myList li.active');
Sarfraz
Thanks to u too..! Works fine..
Amit
@Amit: You are welcome....
Sarfraz