I was thinking of some sort algorithm to perform dynamic array sorting list, but this http://jsfiddle.net/Hms7Y/1/ seem able to solve my problem with cleaner code. But there's still few steps left to be done.
$(document).ready(function() {
$('button').click(function() {
var lvl = $('select').val();
var ref = $('li.level' + lvl).last();
$('<li class="level" '+ lvl + '>' + lvl + ' </li>').insertAfter(ref);
});
});
what if initially I don't have any HTML markup, what is in my head now is use 3 if statement to check whether it's the 1st li when the user insert a li. is there any other better ways?
$('ul').children('li').lengthif it's greater than 0 aliis already present, at least I think. If it's correct you could also set a Boolean to avoid to check the Dom each time