1
I have a table HTML
which I fill with data I own in a database.
Each tag tr
of my table represents a client, disregarding tag tr
header.
I would like to add an event of click to the tags tr
, as if they were selecting a customer.
HTML table :
<table id="modelTable">
<thead>
<tr>
<th>Id User</th>
<th>UserEmail</th>
<th>CodeCustomer</th>
<th>DateCreation</th>
<th>DateContract</th>
<th>Condition</th>
</tr>
</thead>
<tbody></tbody>
</table>
I’m trying this way (Java) :
document.getElementsByTagName("tr").addEventListener('click', function () {
console.log("clique");
});
Error :
Uncaught Typeerror: Document.getelementsbytagname(...). addeventlistener is not a Function At Htmlanchorelement.
I must be missing something, although I’ve spent some time looking, I wish someone could point out to me where I’m going wrong.
It won’t work even when you do this Document.getelementsbytagname("tr") is returning a
nodelist
or an array with all elementstr
document, even if you only have one. Vc prercises access by an index like this for example [0]– LeAndrade
@Leandrade Thank you for the comment. Your suggestion worked in parts, because only this working for the tag tr of index 0. When I add for example the index 1 appears this error : Uncaught Typeerror: Cannot read Property 'addeventlistener' of Undefined at Htmlanchorelement.<Anonymous>
– Levi
Yeah, because there’s only one tr.
– LeAndrade
@ Leandrade But there would be no way to add the click event to the tr tags that are added to the table (createelement("tr")) ?
– Levi
Yeah, sure, but then it’s another scam, you’re gonna have to do one for() going through the entire document and accessing the contents of the.
– LeAndrade