Reputation: 5131
An example of what I have in HTML:
<table id="testME">
<tr>
<td><input id="first" name="choices" type="radio" value="1"></td>
<td><input id="second" name="choices" type="radio" value="2"></td>
<td><input id="third" name="choices" type="radio" value="3"></td>
</tr>
<tr>
<td><label for="first">My first selection</label></td>
<td><label for="second">My second selection</label></td>
<td><label for="third">My third selection</label></td>
</tr>
<tr>
<td><input id="myWords" type="text" /></td>
<td><select id="mySelects">
<option value="30">Choice 1</option>
<option value="31">Choice 2</option>
</select>
</td>
<td><input id="multiOne" name="multiSelect" type="checkbox" value="one" />
<label for="multiOne">One</label><br />
<input id="multiTwo" name="multiSelect" type="checkbox" value="two" />
<label for="multiTwo">Two</label><br />
<input id="multiThree" name="multiSelect" type="checkbox" value="three" />
<label for="multiThree">Three</label><br />
<input id="multiFour" name="multiSelect" type="checkbox" value="four" />
<label for="multiFour">Four</label><br />
</td>
</tr>
</table>
What I would like to happen is when I click on the radiobutton or its label, all of the cells in that column (whether it's th or td) become highlighted a certain color. I'd like to do this with JQuery.
What I researching before I posted this question:
<script>
function highlightSelection() {
$('#testME > tr > td:nth-child(2)').css('background-color', 'gray');
}
</script>
<input id="jquerytest" type="button" onclick="javascript:highlightSelection();" value="Change Color" />@:
But that doesn't seem to work.
Upvotes: 0
Views: 244
Reputation: 146310
Something like this:
$("table").on("click", "input", function() {
var td = $(this).parent("td"),
tdIndex = td.index();
var tr = td.parent("tr"),
trIndex = tr.index();
$("table, table tr, table tr td").css("background", "none");
$("td:eq(" + tdIndex + ")", "tr").css("background", "red");
$("tr:eq(" + trIndex + ")").css("background", "blue");
});
Fiddle: http://jsfiddle.net/maniator/rZqdP/
Upvotes: 2