How to add a list-item at specific position

Posted by Germanico Brismal on Stack Overflow See other posts from Stack Overflow or by Germanico Brismal
Published on 2010-05-28T20:17:26Z Indexed on 2010/05/28 20:21 UTC
Read the original article Hit count: 174

Filed under:
|
|

Hi,

I'd like to add a <li> at a specific position, for example:

<ul id="list">
    <li>Position 1</li>
    <li>Position 2</li>
    <li>Position 4</li>
<ul>

Let's say that I want to add a new <li> below/after <li>Position 2</li>, how can I do it using jQuery?

I've tried to do it using the code below:

$('#list li:eq(1)').append('<li>Position 3</li>');

But, it didn't work, because it appends the <li> inside the <li>Position 2</li>, instead add the <li> below/after the <li>Position 2</li>.

Can someone give me some help?

Thank you.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about append