Alex Rivera | Logout

Javascript access TR from TD

Asked 2010-03-26T15:16:18.220
15

I have a table row, and within that, I have a td (whatever it stands for). I would like to change the class attribute of the TR my TD is in without using an ID or a name. Like that:

<tr>
    <td onclick="[TR].setAttribute('class', 'newName')">My TD</td>
</tr>

How do I do it?

Edit
Report

2 Answers

0

If you can use jQuery it could be something like this

$("yourtdselector").closest("tr").attr("class","classname");

For your code

<tr>
    <td onclick="changeClass(this,'classname')">My TD</td>
</tr>

function changeClass(elem, class)
{
    elem.parentNode.className = class;
}
answered 2010-03-26T15:20:24.780
-3

Without any extra framework:

document.getElementById("theTableName").rows[1].cells[1].className = "someclassname";
answered 2010-03-26T15:21:08.227

Your Answer