I have this table
<table id="tblId">
<thead>
<tr>
<th>View Name</th>
<th>View Description</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>Name</td>
<td><span onclick="jsFunction()">description</span></td>
</tr>
<tr id="2">
<td>Name</td>
<td><span onclick="jsFunction()">description</span></td>
</tr>
</tbody>
</table>
In the onclick event of each span i need to send the js function the row id of that specific row, How do i do that?
Thank you!
To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.
$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding. Traditional method: document.
Parent Element returns null if the parent is not an element node, that is the main difference between parentElement and parentNode. In many cases one can use anyone of them, in most cases, they are the same.
One way is you can send "sender" data with jsFunction as below,
... onclick="jsFunction(this)" ....
In your jsFunction you can find tr element,
function jsFunction(sender){
var tr = sender.parentNode.parentNode;
alert( tr.getAttribute('id') );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With