I'm trying to write a javascript that will toggle the class of a table row on the onClick event so that I can use css to change the look and functionality of it, however I'm not quite sure how to accomplish this.
The goal is to have a table in which users can select a row and have it highlight, and should they select another row the previous selection return to its original class. The table rows have alternating colors, and I am unsure how to get this to work the way I like.
The 3 css classes are: evn odd selected
A row can only be 1 of the 2:
<script>
function toggle(elem) {
selectClass = 'selected';
orgClass = document.getElementById(elem).className;
elem.className = (elem.className == 'selectClass)?orgClass:selectClass;
}
</script>
<table>
<tr class='evn' id=0 name='rowsel' tabindex=0 onClick='toggle(this);'>
<td>something</td>
<td>something else</td>
</tr>
<tr class='odd' id=1 name='rowsel' tabindex=0 onClick='toggle(this);'>
<td>something</td>
<td>something else</td>
</tr>
<tr class='evn' id=2 name='rowsel' tabindex=0 onClick='toggle(this);'>
<td>something</td>
<td>something else</td>
</tr>
</table>
Thanks in advance.