TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-26-2007, 10:49 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Change Table Row Colour with Checkbox onClick

Hello,

I think this may be a really simple task, but I'm no Javascript guru, so I'm having trouble grasping it. Google also wasn't much help, but probably because couldn't describe well enough what I wanted.

Before I start, if anybody does reply and uses PrototypeJS, I also use it and a method using it would be much more preferred, but if not, normal Javascript will do.

So I have this table set up... I'm not much of a table user, but using div's for this would just be too much hassle.

Code:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <thead>
    <tr>
      <th align="left" colspan="2">Message</th>
      <th width="25%">Sender</th>
      <th width="10%">Mark</th>
    </tr>
  </thead>
  {foreach item=pmsg from=$pmno}
  <tbody>
    <tr id="tr" class="{if $pmsg.is_new eq 1}is_new{else}{cycle values="color1, color2"}{/if}">
      <td align="center" width="5%">
      {if $pmsg.is_new eq 1}
      <img src="images/new_pm_folder.png" alt="New PM" title="New PM" />
      {else}
      <img src="images/no_new_pm_folder.png" title="No new PM" alt="No new PM" />
      {/if}
      </td>
      <td><a href="ucpanel/pm/{$pmsg.pm_id}/">{$pmsg.subject}</a><div class="pm_date">{$pmsg.date|date_pretty}</div></td>
      <td align="center"><a href="user/{$pmsg.username|urlencode}/">{$pmsg.username}</a></td>
      <td align="center"><input name="pm_option[]" type="checkbox" id="pm_option" value="{$pmsg.pm_id}" /></td>
    </tr>
  </tbody>
  {/foreach}
</table>
The code between {} is from Smarty.

All I want to do, is when the checkbox is checked, I want the table row to change a different colour, and when unchecked, it will remove the colour... sounds simple huh? but I couldn't work my head around it... I mean I tried for ages and almost got it; I managed to change the colour of it, but when unchecked, it didn't remove it.

Thanks for any help.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-27-2007, 12:52 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Try this. Just set the onclick() action on the checkboxes to call change_row_colour(this). Like so:

Code:
onclick="change_row_colour(this)"
Here's the main part though:

javascript Code:
<script type="text/javascript">

    var szColorTo = '#0000FF';
    var szColorOriginal = '#FF0000';
   
    function change_row_colour(pTarget)
    {
        var pTR = pTarget.parentNode.parentNode;
       
        if(pTR.nodeName.toLowerCase() != 'tr')
        {
            return;
        }
       
        if(pTarget.checked == true)
        {
            pTR.style.backgroundColor = szColorTo;
        }
        else
        {
            pTR.style.backgroundColor = szColorOriginal;
        }
    }

</script>
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 12-05-2007 at 03:46 AM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-27-2007, 01:42 AM   #3 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Cheers Wild Honey, I'll give it a shot and come back with a response.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-27-2007, 03:06 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Any luck with that, laddo?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-27-2007, 04:37 PM   #5 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Aye, much luck!

Thanks ya very muchly!

Wish I knew what half of it meant, lol. What's parentNode and nodeName?
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-27-2007, 05:47 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Hehe. It's DOM so parentNode would be the node above it. So if you have:

Code:
<table>
	<tr>
		<td>Element</td>
	</tr>
</table>
The TD's parent node is the TR, and the parent of the TR is the table node. nodeName is simply its name, TR's node name is TR. Simple as that.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-03-2007, 09:39 PM   #7 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

I've just realised that this doesn't work in Internet Explorer... what's new eh?

I did a slight modification than the code Wildhoney provided:

Code:
<script type="text/javascript">
  var bgColor = '#CCCCCC';
  var bgColorOrig = '#F3F3F3';
  {literal}
  // Change table row on checkbox click
  function colorRow(pTarget) {
    var pTR = pTarget.parentNode.parentNode;
    				
    if(pTarget.checked == true) {
      pTR.setStyle({backgroundColor: bgColor});
      $('delete_pm').enable();
    }
    else {
      pTR.setStyle({backgroundColor: bgColorOrig});
        $('delete_pm').disable();
    }
  }
{/literal}
</script>
Basically just uses Prototype functions instead of normal Javascript. In IE, when I click the checkbox, the colour doesn't change and the submit button stays disabled.

I hate IE sometimes...
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 04-21-2009, 10:34 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I have a question about this part:

Code:
 
var pTR = pTarget.parentNode.parentNode;
        
if(pTR.nodeName.toLowerCase() != 'tr')
You're creating the variable "pTR" and setting it to:

pTarget.parentNode.parentNode

Then using it later in the next line. Are you doing this just to
shorten it so it wouldn't be so long like this?

Code:
if(pTarget.parentNode.parentNode.nodeName.toLowerCase() != 'tr')
Also, how come you're using the toLowerCase() function?
allworknoplay is offline  
Reply With Quote
Old 04-21-2009, 10:37 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I changed the code a little bit, but it doesn't work for me?

Code:
<script type="text/javascript">
	var color_on = '#EEEEEE';
	var color_off = '#FFFFFF';
	
	function row_color(pTarget)
    {
        var pTR = pTarget.parentNode.parentNode;
        
        if(pTR.nodeName.toLowerCase() != 'tr')
        {
            return;
        }
        
        if(pTarget.checked == true)
        {
            pTR.style.backgroundColor = color_on;
        }
        else
        {
            pTR.style.backgroundColor = color_off;
        }
    }
	
	
	
</script>

Code:
<table width="796" border="1" cellspacing="5" cellpadding="2">
  <tr>
    <td width="50"><label>
      <div align="center">
        <input type="checkbox" onclick="row_color(this)" id="checkbox" name="checkbox"  />
        </div>
    </label></td>
    <td width="717">whatever01</td>
  </tr>
  <tr>
    <td><div align="center">
      <input type="checkbox" onclick="row_color(this)" id="checkbox" name="checkbox"  />
    </div></td>
    <td>whatever02</td>
  </tr>
  <tr>
    <td><div align="center">
      <input type="checkbox" onclick="row_color(this)" id="checkbox" name="checkbox"  />
    </div></td>
    <td>whatever03</td>
  </tr>
</table>
allworknoplay is offline  
Reply With Quote
Old 04-21-2009, 11:49 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

For your latest code, the pTR variable isn't pointing to the table row, but to the label (first row) or td (other rows). It goes up two levels in the DOM tree, your HTML code needs it to traverse back four/three levels to reach the table row.

Quote:
how come you're using the toLowerCase()
It's just to make a consistently cased node name across the various browsers.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
allworknoplay (04-22-2009)
Old 04-22-2009, 12:25 AM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
For your latest code, the pTR variable isn't pointing to the table row, but to the label (first row) or td (other rows). It goes up two levels in the DOM tree, your HTML code needs it to traverse back four/three levels to reach the table row.


It's just to make a consistently cased node name across the various browsers.
Thanks Salathe,

Once I removed the label and the div's, it worked.
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design