Shaun Mccran

My digital playground

03
S
E
P
2009

Changing a table row colour with JavaScript

I was recently looking at a way of people selecting many records from a data display. You know the usual table layout of data, populated with a query object. I decided on a column of checkboxes so that a user could select multiple records. The only issue with this was that it is not the most visible indicator of whether a record is selected.

So I thought I would add a JavaScript function to change the table row background colour, to show selected records.

Firstly here is a simple mock up of a table, populated with a list.

view plain print about
1<cfset variables.myList = '1,2,3,4,5,6,7,8,9,10,11,12,13,14'>
2<cfparam name="variables.odd" default="">
3
4<cfoutput>
5<table width="200" border="0" cellspacing="0" cellpadding="2">
6<cfloop list="#variables.myList#" index="variables.row">
7    <cfif (variables.row MOD 2) IS 1>
8        <cfset variables.odd = true>
9    </cfif>
10    <tr <cfif variables.odd> bgcolor="CCCCCC" </cfif> >
11        <td>Row #variables.row#</td>
12        <td>Data 2</td>
13        <td align="middle"><input type="checkbox" id="rowId" name="rowId" value="#variables.row#" onclick="row_color(this,#variables.odd#)"></td>
14    </tr>
15    <cfset variables.odd = false>
16</cfloop>
17</table>
18</cfoutput>

The table rows are alternately coloured based on a basic odd/even check. This value is also passed to the function, as if we uncheck the record we want the row to return to its original colour.

Next we have the JavaScript function that each checkbox is running when it is clicked. The 'row_color' function uses the 'this' variable to work back up two elements using the 'parentNode' function. This targets the table row (tr) tag, and changes its colour to whichever value is set at the top of the script. There are two colour off values as the table rows are alternately coloured, and we want them to return to their original colour when unselected.

view plain print about
1<scr/ipt type="text/javascript">
2    var color_on = '#FFE7DF';
3    var color_off_1 = '#CCCCCC';
4    var color_off_2 = '#FFFFFF';
5    
6    function row_color(pTarget,row)
7    {
8        var pTR = pTarget.parentNode.parentNode;
9
10        if(pTR.nodeName.toLowerCase() != 'tr')
11        {
12            return;
13        }
14
15        if(pTarget.checked == true)
16        {
17            pTR.style.backgroundColor = color_on;
18        }
19        else
20        {
21            if(row == 1)
22            {
23                pTR.style.backgroundColor = color_off_1;
24            }
25            else
26            {
27                pTR.style.backgroundColor = color_off_2;
28            }
29            
30        }
31    }
32</scr/ipt>

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
drew's Gravatar Just a suggestion to add a pro form tip to use jQuery, dojo, mooTools, anything.
This is definitely awesome in the case that you are limited to using only javascript.
# Posted By drew | 03/09/2009 13:26
drew's Gravatar Should be "pro forma". I haven't had the chance to practice my latin recently.
# Posted By drew | 03/09/2009 13:27
Back to top