views:

45

answers:

3

I have an array called currentTD that holds two integers, the first element in the array is an ID to a <TR> element, and the second element in the array is the ID of a <TD> element within that <TR>.

I want to somehow select that <TD> and change its background colour. I'm just not sure how I can select that <TD> given the "co-ordinates" from currentTD?

Thanks.

A: 
$('#' + currentTD[1]).css('background-color', color);
Matthew Flaschen
+2  A: 

This is based on your previous question: you can use .eq(), like this:

$("table").find("tr").eq(trIndex).children("td").eq(tdIndex)
          .css({ backgroundColor: "red" });

This assumes you have an array like this: [tdIndex, trIndex]. As for your previous markup, remove those IDs, they're both invalid and not needed.

IDs can't start with a number (unless you're using HTML5) and can't be repeated, as they are on your <td>s. But since you can get what you want via indexes...there's no need for the id attributes anyway, so just remove them.

Nick Craver
@Nick - I think the OP want `id` s...
Reigel
@Reigel - The word "coordinates" put's that in doubt for me, I think what he has is "row 12, cell 4" in an array...of course I'm guessing here, could be totally wrong.
Nick Craver
+1...... The OP should reconsider his markup...
Reigel
A: 

If currentTD has an id of the TD to change it's background as it's second element, then the following should work.

$('#' + currentTD[1]).css('background-color', 'red');
Jason McCreary