views:

550

answers:

6

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

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

How do I do it?

A: 

jQuery is probably the easiest way of doing this, you can use selectors such as:

$('table.mytable tr').addClass('red');

To add a class of 'red' to all tr's in table.mytable. That's just the tip of the iceberg - check it out it should do what you need.

DCD
That is not actually answering the question at all. You basically just told him to go look up how to use jQuery. Not useful at all.
Keith Rousseau
+1  A: 

In jquery, it would be really simple if you have the reference to your td:

$(this).closest('tr');

If you really don't want to take a dependency on jQuery, then you could just do a loop getting the parentNode and checking it's type as a more general purpose solution. In this case you could just get the parentNode since tr is always a direct parent of td. You can do something like this (note this was not tested):

var parent = myTd.parentNode;
while(true) {
  if(parent == null) {
    return;
  }
  if(parent.nodeName = "TR") {
    return parent;
  }
  parent = parent.parentNode;
}
Keith Rousseau
you realise that if you have the HTML <tr><td></td><td></td><td></td></tr> then all of those <td> share the same parent, they are not nested under each other. They are siblings.
thecoshman
Yeah, I fixed the answer to note that mine is a more general purpose script.
Keith Rousseau
Should be parent.nodeName === "TR" rather than parent.nodeName = "TD"
Ms2ger
`tagName`/`nodeName` comparisons should always be case-insensitive (ie. `nodeName.toLowerCase()==='tr'`), you shouldn't rely on browsers changing the case. I'm not really sure what the point of looking up the ancestor list is when you've got a `<td>`: its direct parent will always be the `<tr>`.
bobince
I was thinking more from the point of where you click on a link in a td or something. You typically aren't actually clicking on the td itself. And FWIW, nodeName is always all capitals so doing toLowerCase is redundant.
Keith Rousseau
@bobince Whilst the HTML spec may or may not say that a TD should only be bested with in a TR it may not always be the case. I have played with trying to get a table body to scroll, and that involved nesting the TD with in a DIV. It's unlikely, but may happen.
thecoshman
@thecoshman: It doesn't happen. The browser ‘fixes’ the table up for you so that `<td>` is inside `<tr>` is inside `<tbody>`, regardless of what your input was. Different browsers may apply different fixes for invalid input (which is why you shouldn't rely on it), but alert the `innerHTML` and you will see the proper expected nesting.
bobince
A: 

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;
}
rahul
A: 

Without any extra framework:

document.getElementById("theTableName").rows[1].cells[1].className = "someclassname";
Joop
That's not getting it based on a td.
Keith Rousseau
A: 

if you are have the dom element in javascript, you can use .parentNode() then which will give you the perant node, which should be the table row. then you can set .className

thecoshman
It's actually parentNode.
Keith Rousseau
Thanks, updated.
thecoshman
+13  A: 

td stands for table data..

now .. in your case you need the parentNode attribute of the td ..

<tr>
<td onclick="this.parentNode.setAttribute('class', 'newName')">My TD</td>
</tr>

or as bobince suggested in his comment

<td onclick="this.parentNode.className= 'newName'">My TD</td>
Gaby
+1 for solving problem with out using ID (like requested in question) or relying on Jquery
thecoshman
PERFECT!table data... I see. Thank you!
arik-so
`setAttribute('class')` doesn't work right in IE6-7. There are many attributes where IE gets it wrong: avoid `getAttribute`/`setAttribute` in HTML documents for this reason. Instead, use DOM Level 1 HTML properties: `this.parentNode.className= 'newName'` is more compatible and more readable.
bobince
thanks for that one. I daresay I find it extremely annoying how IE simply overrides the standards. ^^
arik-so
@bobince, thanks for the comment, you are absolutely right.
Gaby
@arik-so — this isn't overriding the standards, it is a simple bug. It is fixed in IE8.
David Dorward