tags:

views:

44

answers:

1

I want to add an LI to my UL list below above the .last-child:

<ul id="list">
    <li>item 1<li>
    <li>item 2<li>
    <li>item 3<li>
    <li class="last-child"><li>
</ul>

I am using the following JQuery code to do so:

var result = '<li>item 4</li>';
$("#list li.last-child").before(result);

That all works fine. However, I want to select the newly added row... Normally I guess I could use the following code to select the last LI:

var $newrow = $('#list').find('li:last');

But in this case I want to select the second last LI... How do I select the second last LI which is the one I just inserted?

Am I making sense?

Thanks in advance!

+2  A: 
var result = $('<li>item 4</li>');
$('#list li:last-child').before( result );

// result reference is saved, so keep on using result.

// However you could have used .prev() to anchor to the previous element.
meder