2

I am adding table in html through jquery dynamically. I am able to add. But I can't remove added table. CSS 'remove' id which is appended from jquery doesn't work. 'remove' Id is added in section tag at last of script

Jquery

    $().ready(function(){
        $('#AddMoreId').on('click', function(){
                $('article:last').append('<article class="col100 left bggreen"><table><tr><th>ID</th><td><input type="number" id="id"></td></tr><tr><th>Menu in NPL</th><td><input type="text" id="menunep"></input></td></tr><tr><th>Menu in ENG</th><td><input type="text" id="menueng"></input></td></tr></table><section class="col100 left cursorpointer" id="remove">Remove</section></article><br/>');
        });

        $('#remove').on('click', function(){ 
            $(this).parents('article').remove();
        });

    });

Html

<article class="col100 left bggreen">
    <table>
        <tr>
            <th>
                ID
            </th>
            <td>
                <input type="number" id="id">
            </td>
        </tr>
        <tr>
            <th>
                Menu in NPL
            </th>
            <td>
                <input type="text" id="menunep"></input>
            </td>
        </tr>
        <tr>
            <th>
                Menu in ENG
            </th>
            <td>
                <input type="text" id="menueng"></input>
            </td>
        </tr>
        <tr>
            <td class="cursorpointer" colspan="2" id="AddMoreId">Add field
            </td>
        </tr>
    </table>
</article>
Dipak
  • 931
  • 14
  • 33

4 Answers4

3

For dynamically added items, use the following:

$(document).on('click', '#remove', function(){ 
    $(this).parents('article').remove();
});
Eliellel
  • 1,426
  • 1
  • 9
  • 14
1
$(document).on('click', '.remove', function(){ 
    $(this).parents('article').remove();
});

Also you should note that you will have multiple buttons with id "remove", which is considered invalid, you should change it to use class instead.

Jeremy
  • 463
  • 1
  • 4
  • 10
1

try following code.

  1. you need change id to class
  2. use closest instead of parent

$().ready(function() {
        $('#AddMoreId').on('click', function(){
                $('article:last').append('<article class="col100 left bggreen"><table><tr><th>ID</th><td><input type="number" id="id"></td></tr><tr><th>Menu in NPL</th><td><input type="text" id="menunep"></input></td></tr><tr><th>Menu in ENG</th><td><input type="text" id="menueng"></input></td></tr></table><section class="col100 left cursorpointer remove1" >Remove</section></article><br/>');
        });

      $(document).on('click', '.remove1', function(){ 
       $(this).closest('article').remove();
     });

   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article class="col100 left bggreen">
    <table>
        <tr>
            <th>
                ID
            </th>
            <td>
                <input type="number" id="id">
            </td>
        </tr>
        <tr>
            <th>
                Menu in NPL
            </th>
            <td>
                <input type="text" id="menunep" />
            </td>
        </tr>
        <tr>
            <th>
                Menu in ENG
            </th>
            <td>
                <input type="text" id="menueng" />
            </td>
        </tr>
        <tr>
            <td class="cursorpointer" colspan="2" id="AddMoreId">Add field
            </td>
        </tr>
    </table>
</article>
patelarpan
  • 7,188
  • 2
  • 20
  • 25
0

That is because the id is being generated dynamically. For this to work well, you need to modify the code as follows:

$('.bggreen').on('click', '#remove', function(){ 
    $(this).parents('article').remove();
});
Abin Thaha
  • 4,493
  • 3
  • 13
  • 41