How do I click a button and remove the table row via javascript?

Asked

Viewed 252 times

-3

Hi, how do I click a button and remove the table row via javascript? The most I can do is remove a cell.

HTML :

inserir a descrição da imagem aqui

JAVASCRIPT:

inserir a descrição da imagem aqui

result when you click the button :

inserir a descrição da imagem aqui

  • 1

    And there, Caião. Beauty ? Avoid putting code print, and yes, copy the code and put it. That people copy your code and reproduce to try to find a solution for you.

  • I try to put, but the page reads the html in the shows it

  • There is an icon next to the image insert icon (on the right side). There you should insert HTML, CSS, Javascript codes.

  • Puts html any way we fix it. But just to get to know the text here is formatted with markdown.

  • What is markdown ?

  • I understand now, thank you very much!!!!

  • @Gambi https://guides.github.com/features/mastering-markdown/

Show 2 more comments

1 answer

1


Speak up, Caião. See if it helps you.

function removerElemento(elementoClicado) {
  elementoClicado.closest("tr").remove();
}
<table border="2" id="tabela">
  <thead>
    <tr class="tr-cabecalho">
      <th>Nome do Produto</th>
      <th>Descrição</th>
      <th>Qtd Estoque</th>
      <th>Valor</th>
      <th>Descontos</th>
      <th>Ações</th>
    </tr>
  </thead>

  <tbody>
    <tr class="tr-corpo-1">
      <td>Avast</td>
      <td>Protetor Antivírus</td>
      <td>5</td>
      <td>R$ 30.00</td>
      <td>0</td>
      <td nowrap>
        <button class="editar">Editar</button>
        <button class="apagar" onclick="removerElemento(event.target)">Apagar</button>
      </td>
    </tr>
    <tr class="tr-corpo-2">
      <td>Word</td>
      <td>Editor de Texto</td>
      <td>5</td>
      <td>R$ 250.00</td>
      <td>0</td>
      <td nowrap>
        <button class="editar">Editar</button>
        <button class="apagar" onclick="removerElemento(event.target)">Apagar</button>
      </td>
    </tr>
  </tbody>
</table>

In the code above, in HTML, when we click on the element (button), we send it itself by parameter in the function removeElement(parameter).

In the Javascript, we receive this element and Closest(), We’re looking for your nearest ancestor "tr".

Finally, we use the method remove() (JS native) to remove this "tr".

Reference of the Closest: Closest

If you need anything, let me know. Hugging !

  • Thank you very much!!!! I will test

  • Nothing, Caio. Anything we are there. If, after testing, the answer is useful for you, just click on the "V" there to accept the answer. But do this only if the answer solves your problem completely. Any questions, just mention here that I edit the answer. May the force be with us !

  • I just tested and it worked perfectly, thank you very much GAMBI

  • Ambi, you could help me with something else?

  • Sure, brother. If I know. Go ahead.

  • I just talked to you by outlook, I am not able to ask question here.

  • You have the name of my best friend. All Caio are good people. Make yourself at home.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.