Take the value of the "data-" attribute with jquery

Asked

Viewed 138 times

0

Good afternoon, I am doing an html + js activity (with jquery), I need to get the value of the attribute "date-" when the button is clicked. Follow the code:

html:

    <table id="table" class="table table-hover">
  <thead>
    <tr>
      <td>Foto</td>
      <td>Produto</td>
      <td>Descrição</td>
      <td>Tipo</td>
      <td>Alocação</td>
      <td>Quantitativo</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><img src="http://localhost:7304/storage/img/aa76a3cbb48e4c85836f9464fca46e15.jpg" class="images" width="100px" height="100px"></td>
      <td>Luva</td>
      <td>feitos para usar individualmente</td>
      <td>EPC</td>
      <td>Prateleira 2/Linha 1</td>
      <td data-name="1"><button type="button" class="quantitative-btn">Ver Quantitativo</button></td>
    </tr>
    <tr>
      <td><img src="http://localhost:7304/storage/img/QIpAROTIjsoZZgxlDFaHRRgmQa1ndhNI6DUjg12M.jpeg" class="images" width="100px" height="100px"></td>
      <td>print</td>
      <td>345tyuiop</td>
      <td>EPC</td>
      <td>Prateleira 1/Linha 1</td>
      <td data-name="2"><button type="button" class="quantitative-btn">Ver Quantitativo</button></td>
    </tr>
  </tbody>
</table>

js:

$(() => {
  $(document).on('click', '.quantitative-btn', (e) => {
    e.preventDefault();
    var id = $(this).parent().parent().find('td').data('name');
    alert(id);
  });
});

When the button is clicked, it appears "null". How can I resolve this?

1 answer

2


You can try:

$('.quantitative-btn').on('click', function(){
     var name = $(this).parent().data('name');
     alert(name);
});
  • Hello Allex, in Alert it returns "Undefined"...

  • 1

    Friend, I tested it here and it worked Result: https://snipboard.io/G0BibL.jpg Code: https://snipboard.io/rCMa1R.jpg

  • 1

    I had forgotten to delete the page cache in the update, but thanks, it worked.

  • 1

    dispose friend.

Browser other questions tagged

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