how to insert a new li tag at the specified lication?

Posted by Amit on Stack Overflow See other posts from Stack Overflow or by Amit
Published on 2010-04-05T11:11:29Z Indexed on 2010/04/05 11:13 UTC
Read the original article Hit count: 315

Filed under:

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?

© Stack Overflow or respective owner

Related posts about jQuery