Search code examples
javascriptjqueryhtml-tablerow

Remove table row after clicking table row delete button


Solution can use jQuery or be plain JavaScript.

I want to remove a table row after user has clicked the corresponding button contained in the table row cell so for example:

<script>
function SomeDeleteRowFunction() {
 //no clue what to put here?
}
</script>

<table>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
</table>

Solution

  • You can use jQuery click instead of using onclick attribute, Try the following:

    $('table').on('click', 'input[type="button"]', function(e){
       $(this).closest('tr').remove()
    })
    

    Demo