Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get element by data attribute

I have this html structure:

<table id="table">
   <tr data-id="3">
      <td>08.09.2021</td>
      <td><div class="btnDelete">Delete</div></td>
   </tr>
</table>

and this onclick function:

var myID = 3;
$(".btnDelete").click(function() {
    
   console.log(
      $('#table').parents('tr').find("[data-id='" + myID + "']").html()
   )

})

Result will be "undefined".
I would like to get the html part of <tr data-id="3"> and search this element by the data attribute "data-id". But my way doesn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Where is my mistake?

>Solution :

Here is your error:

  $('#table').parents('tr').find("[data-id='" + myID + "']").html()

That should be:

  $('#table').find("[data-id='" + myID + "']").html()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading