View Full Version : how do I grab <td> value if the <td> has no id?

02-28-2009, 11:43 PM
I'm trying to compare one <td> to a large set of other <td>s, but how do I get at the <td> values if there are only class attributes, and no id attributes, associated with the large list of <td>s? Example HTML:

<td class="filled rd2">Team 1</td>
<td class="filled rd2">Team 2</td>

<td id="myteam">Team 1</td>

I want to find out if the value of the <td> with the id of "myteam" is the same as the value of ANY of the <td>s with the class of "rd2". Can I create a new array out of the <td>s that share the "rd2" class?

Thanks for any help.

03-01-2009, 01:08 AM
Like this?:

<table><tr><td class="hi">Hello</td><td>GoodBye!</td></tr></table>
<script type="text/javascript">
for(i = 0; (a = document.getElementsByTagName("td")[i]); i++){
if(a.className = "hi"){
a.innerHTML = "Hi!";

03-01-2009, 02:35 PM
hey Nile, thanks, but I'm not sure. a few things:

I don't want to change any HTML, I just want to compare. I have a NCAA bracket website, and if someone's bracket cell has the same value (i.e. the same winning team) as the correct bracket, then I want to add a class (class="correct") to that cell. So that's where the comparing comes in.

Also, is .className a jQuery maneuver, or is it straight JS?

03-01-2009, 03:34 PM
.className is "straight" js... I'll see what I can do with the comparison things.

03-01-2009, 03:49 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.correct {
background-color: #ee8;
<table><tr><td class="filled rd2">Team 1</td></tr>
<tr><td class="filled rd2">Team 2</td></tr>

<tr><td id="myteam">Team 1</td></tr></table>
<script type="text/javascript">
for(var i = 0, w = document.getElementById('myteam'), a; (a = document.getElementsByTagName("td")[i]); ++i){
if(/\brd2\b/.test(a.className) && a.firstChild.nodeValue == w.firstChild.nodeValue){
a.className += ' correct';