I liked the reply of Diego Vieira(+1) because I did not know this attribute date-something that can be created.
However, I went a different way, because I saw that the name was already written inside the td and I tried to catch it with innerHTML
. I could not with the code used because it is a Jquery object and is not so with .innerHTML
that works with Jquery. Jquery creates an encapsulation for content that is different from using elemento = document.getElementById("iddoelmento");
.
Then a curiosity arose: How could I get the names of properties and methods of objects of any nature, whether jquery or not? I discovered the existence of Object.getOwnPropertyNames(nomedoobjeto)
when searching Stack Overflow in English.
In the test in question the object located with Jquery had the following properties:
0,1,length,prevObject,context,selector
I started testing 0 and it was the element I needed. tdobj[0].innerHTML
returns the contents of the located cell.
I would like to share the experience, follow the executable code below.
$(function(){
$(document).on('click', '.btn-danger', function(e) {
e.preventDefault;
//tdobj = $(this).parent().parent().find('td');
tdobj = $(this).closest('tr').find('td');
// lalala = Object.getOwnPropertyNames(tdobj);
// alert(lalala);
// 0,1,length,prevObject,context,selector
alert("[0]: " + tdobj[0]);
alert("[0].innerHTML:\n" + tdobj[0].innerHTML);
alert("[1]: " + tdobj[1]);
alert("[1].innerHTML:\n" + tdobj[1].innerHTML);
alert("length: " + tdobj["length"]);
alert("prevObject: " + tdobj["prevObject"]);
alert("context: " + tdobj["context"]);
alert("selector: " + tdobj["selector"]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Teste Nome do Cara</td>
<td><button class="btn-danger">Recuperar nome</button></td>
</tr>
</table>
Reference: https://stackoverflow.com/questions/2257993/how-to-display-all-methods-of-an-object-in-javascript?answertab=active#tab-top
I think use
.closest('tr')
is safer than.parent().parent()
n times.– Sergio