...

View Full Version : Help



SAFCliam
07-19-2011, 11:01 PM
I have a table as follows

<tr>
<td><input class='checkRow' type='checkbox' name='radio[]' value='<? print $id; ?>'></td>
<td>My Name</td>
<td>Date</td>
</tr>


i want some code so when the checkbox is clicked it will change the background of the row!

sunfighter
07-20-2011, 02:04 AM
<table>
<tr id="row" style="background:yellow;">
<td><input type="checkbox" onclick="document.getElementById('row').style.background = '#CC9999'" /></td>
<td>My Name</td>
<td>Date</td>
</tr>
</table>

Philip M
07-21-2011, 07:46 AM
Presumably you wish to toggle the color back again if the box is unchecked:


<table>
<tr id="row" style="background:yellow;">
<td><input type="checkbox" onclick="togglecolor()" /></td>
<td>My Name</td>
<td>Date</td>
</tr>
</table>


<script type = "text/javascript">
var count = 0;
function togglecolor() {
if (count%2 == 0) {
document.getElementById('row').style.background = 'blue'
}
else {
document.getElementById('row').style.background = 'yellow'
}
count ++;
}

</script>



"Dives sum, si non redo eis quibus debeo. - I am a rich man as long as I do not pay my creditors"

bullant
07-21-2011, 12:16 PM
Imo a better way to do this is not give the row an id because it doesn't need one to do what you want. Only give the row an id if it needs one for something else.

When the checkbox is clicked you can iterate up through the DOM from the checkbox until you find its <tr> (using .parentNode). When you find the object reference for the checkbox's <tr> you can then simply set the <tr>'s colour according to whether the checkbox is currently checked or not.

Using this method you are not tying your function to an id and you don't have to pass an id to the function either - and so you can use the same function for all the checkboxes on each row without having to mess with id's for the rows.

You also don't need a counter to check if a checkbox is checked or not. You can simply look at the value of the checkbox's checked attribute. If there is a "select all" checkbox that checks all the checkboxes then your counter will most probably be out of sync. when the "Select all" checkbox is clicked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum