• Question 1
    Geek Answers Handbook

    How to remove the last <li> </li> using jQuery?

    <div id="q"> <ul><li> <table><tr><td>Question 1</td><td><input type="text" name="question1" size="60" /></td></tr> <tr><td>Answer:</td><td><input type="text" name="answer1" size="8" /></td></tr> </li></ul> </div> <input class="btn" id="addquestion" type="button" value="Add Question" /> <input class="btn" id="removequestion" type="button" value="Remove Question" /> <script> $('#addquestion').click(function() { var $question_number = $('#q li').size() + 1; $html='<li><table><tr><td>Question '+$question_number+'</td><td><input type="text" name="question'+$question_number+'" size="60" /></td></tr>\ <tr><td>Answer:</td><td><input type="text" name="answer'+$question_number+'" size="8" /></td></tr></li>'; $('#q ul').append($html); }); $('#addquestion').click(function() { $('#q li:last').remove(); }); </script> 

    $ ('# q li: last'). remove (); does not work. What's wrong?

    +7
    jquery
    Steven Nov 15 '09 at 6:52
    source share
    2 answers

    If you want to remove the last li element inside #d , you can simply use the :last selector:

     $('#q li:last').remove(); 

    If you want to remove every last child li from its immediate parent ( ul elements if you have more than one), you can use :last-child selector:

     $('#q li:last-child').remove(); 
    +15
    CMS Nov 15 '09 at 7:39
    source share

    Try:

     var lis = $('#q ul').children(); // get all the children $(lis.get(lis.length-1)).remove(); // remove the last one 
    +4
    jtbandes Nov 15 '09 at 7:03
    source share

    More articles:

    • Stop the "throbber of doom" browser when loading a comet / server, click XMLHttpRequest - javascript
    • What does it mean to implement a wrapper method? - java
    • look for tags like stackoverflow? - javascript
    • DataGridTextColumn.MaxLength? - wpf
    • When can there be data in both $ _GET and $ _POST - php
    • What tool can catch buffer overflow in C? - c
    • Collection of sets that do not contain sets that are a subset of the other in the collection - set
    • Dict has a list key - python
    • Language for creating flowcharts - dsl
    • how to save a request and send it in an Android application - android

    All Articles

    Geek Answers | 2019