Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2005, 06:46 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I change a row colour with a checkbox?

Hello

I was wondering if someone could help me with the following problem...

I have some data from a database showing on screen - there are 3 rows of data (3 records). Within each row i have a checkbox. If the checkbox is 'ticked' i would like the row colour to change. If the checkbox is 'unticked' i would like the row colour to change back to the original colour.

Any idea how to do this? i'm not very hot on Javascript!

Thanks in advance

Lucy

 
Old November 14th, 2005, 07:32 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

<script>

function highlight()
{

var el = document.getElementByID('picco')
el.style.backgroundColor = 'FFCC00';
}
</script>

<input type=checkbox name=highlight onclick=highlight();>
<tr id"picco">
<td>TEST</td>
</tr>


www.crmpicco.co.uk
 
Old November 15th, 2005, 06:09 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hiya

I've tried this and it doesn't seem to work. I get an error on the line:

var el = document.getElementByID('picco')

Any ideas?

Cheers

Lucy

 
Old November 15th, 2005, 06:22 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Lucy,

Try this...
Code:
<script language="javascript">
function HighlightRowIfChecked(checkBox){
    var bgColor = checkBox.checked ? "yellow" : "white";
    var el = checkBox.parentNode;
    while(el.tagName.toLowerCase() != "tr"){
        el = el.parentNode;
    }
    el.style.backgroundColor = bgColor;
}
</script>

...

<table>
    <tr>
        <td>text</td>
        <td><input type="checkbox" onclick="HighlightRowIfChecked(this);"></td>
    </tr>
</table>
HTH,

Chris

 
Old November 15th, 2005, 08:53 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

you need to change the line:

<tr id"picco"> to
Code:
<tr id="picco">
www.crmpicco.co.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Table row background colour changing meetravig Java GUI 0 September 21st, 2007 05:06 AM
Row colour change with Checkbox Toiletbrush Infopath 3 July 10th, 2007 06:25 AM
how to change the colour of a cell in a jtable Tomi Java GUI 1 April 25th, 2007 03:35 AM
change the colour of some particular rows bimal ADO.NET 1 September 10th, 2004 02:31 AM
Is there a way to change the colour of a tab ctrl? snowydust VB How-To 0 August 10th, 2004 01:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.