3

What is the best way to insert a new li element into a specific position of a nested set model?

For example, the following list:

<ol class="nested">
    <li id="list_1"></li>
    <li id="list_2">
        <ol>
            <li id="list_3"></li>
            <li id="list_4"></li>
        </ol>
    </li>
    <li id="list_5"></li>
</ol>

Let's say I want to insert a new li as a child of #list_2 I can do like that:

$('#list_'+parent_ID+' ol > li:last-child').after('<li id=""></li>');

But wouldn't work if parent was going to be #list_1 or #list_4 Any ideas !?

eduardev
  • 473
  • 8
  • 26
  • So if you apply this function to `
  • ` you end up with `
    1. new li
  • ` but if you apply it to `
  • ` you get `
    1. new ol and li
  • `? – artlung Oct 03 '11 at 21:44
  • @artlung that's what i'm looking for, yes =) – eduardev Oct 03 '11 at 21:48