I liked the response of Diego Vieira (+1) because I did not know this attribute any date-anything that can be created.
However, I went through a different path because I saw that the name was already written inside the TD and I was trying to get it with innerHTML
. I could not with the code used because it is a JQuery object and not with .innerHTML
that works with Jquery. Jquery creates an encapsulation for content that is different from using elemento = document.getElementById("iddoelmento");
.
Then I had a curiosity: How could I get the names of properties and methods of objects of any kind, whether jquery or not? I discovered Object.getOwnPropertyNames(nomedoobjeto)
when searching for 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 by testing 0 and it was the element I needed. tdobj[0].innerHTML
returns the contents of the localized 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>TesteNomedoCara</td><td><buttonclass="btn-danger">Recuperar nome</button></td>
</tr>
</table>
Reference: link