Delete a specific row from an html table with jquery

5

Deleting the row from this table:

<table>
<thead>
<tr><th>Cod.</th><th>Nome</th><th></th></tr>
</thead>
<tbody>
<tr class="tbl1" data-id="1"><td>1</td><td>João</td><td><a href="#" class="excluir" data-id=1/>EXCLUIR</a></td></tr>
<tr class="tbl1" data-id="2"><td>1</td><td>Ronaldo</td><td><a href="#" class="excluir" data-id=2/>EXCLUIR</a></td></tr>
<tr class="tbl1" data-id="3"><td>1</td><td>Silvia</td><td><a href="#" class="excluir" data-id=3/>EXCLUIR</a></td></tr>
</tbody>
</table>

For example, the table row id = 2?

    
asked by anonymous 11.10.2016 / 20:10

3 answers

7

To remove dynamically you can do:

$(".excluir").click(function() {
  $(this).parents('tr').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><thead><tr><th>Cod.</th><th>Nome</th></tr></thead><tbody><tr><th>1</th><td>João</td><td><ahref="#" class="excluir">EXCLUIR</a>
      </td>
    </tr>
    <tr>
      <th>2</th>
      <td>Ronaldo</td>
      <td><a href="#" class="excluir">EXCLUIR</a>
      </td>
    </tr>
    <tr>
      <th>3</th>
      <td>Silvia</td>
      <td><a href="#" class="excluir">EXCLUIR</a>
      </td>
    </tr>
  </tbody>
</table>
    
11.10.2016 / 20:38
3

Try this:

$('[data-id="2"]').remove();
    
11.10.2016 / 20:17
3

To delete a table row by its id:

$('#iddalinha').hide();

But in your case the id you want is a date attribute:

$('tr[data-id="iddalinha"]').hide();
    
11.10.2016 / 20:32