How to delete a selected row in a html table by using jQuery?

alok shreevathsa

I'm trying to delete a selected row in a table using jQuery.

This is my html markup:

<table id="resultTable">
 <tr id="first">
  <td>c1</td>      
  <td>c2</td>      
 </tr>
 <tr id="second">
  <td>c3</td>      
  <td>c4</td>      
  </tr>    
</table>
<button id="del">Clear</button>

This is the script I'm using..

<script>
$(document).ready(function () {   
     var tid="";   

     $('#resultTable tr').click(function (event) {
        tid=$(this).attr('id');
          alert(tid); //trying to alert id of the clicked row   
     });

     $("#del").click(function(){
     alert("removing "+ tid);
            $(tid).remove();    
     });
 });
</script>

I'm declaring a global variable "tid" to obtain "rowId" and using the same variable in both click() function. But I'm unable to delete the corresponding row, please help me

Rayon

Concatenate # to make it valid ID selector, without #, jQuery will look for <first>/<second> elements

$(document).ready(function() {
  var tid = "";
  $('#resultTable tr').click(function(event) {
    tid = $(this).attr('id');
  });
  $("#del").click(function() {
    console.log(tid);
    if ($('#' + tid).length) {
      $('#' + tid).remove();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="resultTable">
  <tr id="first">
    <td>c1</td>
    <td>c2</td>
  </tr>
  <tr id="second">
    <td>c3</td>
    <td>c4</td>
  </tr>
</table>
<button id="del">Clear</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Table add row, edit row, save row, delete row Using HTML, CSS, JS

From Dev

How do I delete a particular row of a table using jQuery?

From Dev

unable to delete row from table using jquery?

From Dev

Delete specific row in CSV file using HTML Table

From Dev

Delete dynamically-generated table row using jQuery

From Dev

How to calculating column and row sum in html table using jquery

From Dev

delete table row dynamically using jQuery in asp.net mvc

From Dev

Delete selected row from table in mysql

From Dev

how to delete the specified row from html table and also in mysql table using php

From Dev

using jQuery drag + drop to delete a row from an SQL table

From Dev

how to highlight the color of selected table row using the name attribute in jquery

From Dev

How to remove unmatched row in html table using jquery

From Dev

On click delete table row in Jquery

From Dev

AJAX Delete row in table with PHP id selected

From Dev

How to delete a row in a table using jQuery?

From Dev

How to insert value into a html table row by row from a popup modal using jQuery

From Dev

How to delete selected row of jTable in database using Hibernate?

From Dev

How to delete a specific row of a dynamic table using JQuery

From Dev

How to delete a specific row of a dynamic table after prompting the user using JQuery?

From Dev

unable to delete row from table using jquery?

From Dev

delete any table row using jQuery

From Dev

Unable to delete row from html table using jquery

From Dev

Delete selected row from table in mysql

From Dev

Delete selected row in jquery datatable

From Dev

How to delete table certain table row created from dynamic dataTables using jquery?

From Dev

How to find selected value of upper row of a table with jquery?

From Dev

How to insert value into a html table row by row from a popup modal using jQuery

From Dev

How to get selected row in html table which is binded by sql using jquery and webservice?

From Dev

How to Delete checked row in MVC using JQuery

Related Related

  1. 1

    Table add row, edit row, save row, delete row Using HTML, CSS, JS

  2. 2

    How do I delete a particular row of a table using jQuery?

  3. 3

    unable to delete row from table using jquery?

  4. 4

    Delete specific row in CSV file using HTML Table

  5. 5

    Delete dynamically-generated table row using jQuery

  6. 6

    How to calculating column and row sum in html table using jquery

  7. 7

    delete table row dynamically using jQuery in asp.net mvc

  8. 8

    Delete selected row from table in mysql

  9. 9

    how to delete the specified row from html table and also in mysql table using php

  10. 10

    using jQuery drag + drop to delete a row from an SQL table

  11. 11

    how to highlight the color of selected table row using the name attribute in jquery

  12. 12

    How to remove unmatched row in html table using jquery

  13. 13

    On click delete table row in Jquery

  14. 14

    AJAX Delete row in table with PHP id selected

  15. 15

    How to delete a row in a table using jQuery?

  16. 16

    How to insert value into a html table row by row from a popup modal using jQuery

  17. 17

    How to delete selected row of jTable in database using Hibernate?

  18. 18

    How to delete a specific row of a dynamic table using JQuery

  19. 19

    How to delete a specific row of a dynamic table after prompting the user using JQuery?

  20. 20

    unable to delete row from table using jquery?

  21. 21

    delete any table row using jQuery

  22. 22

    Unable to delete row from html table using jquery

  23. 23

    Delete selected row from table in mysql

  24. 24

    Delete selected row in jquery datatable

  25. 25

    How to delete table certain table row created from dynamic dataTables using jquery?

  26. 26

    How to find selected value of upper row of a table with jquery?

  27. 27

    How to insert value into a html table row by row from a popup modal using jQuery

  28. 28

    How to get selected row in html table which is binded by sql using jquery and webservice?

  29. 29

    How to Delete checked row in MVC using JQuery

HotTag

Archive