Davide Fish Davide Fish - 7 months ago 26
HTML Question

jQuery last-child and insetBefore does not work

My code:

$('#discussion li:last-child').insertBefore('<li class="other">ciao</li>');


The above does not work. This erases the last
<li>
element.


I wish it add another
<li>
after the last
<li>


Here's a complete example
https://jsfiddle.net/4q0eLkd1/13/

Answer

See this

$(document).on("click","#bott",function(){
    $('<li class="other">ciao</li>').insertAfter("#discussion li:last-child");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<ol id="discussion">
    <li class="self">ciao</li>
</ol>
<button id="bott">click</button>