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 12-27-2008, 06:42 PM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default How to locate certain checkboxes

I have a form that dynamically creates checkboxes. I want to put a checkbox above those so that the user can click on it to check/uncheck all of them at once. But I can't figure out how to find the checkboxes. Here's what I am trying to do:

This creates the single checkbox that I want to click to toggle all the others.
PHP Code:
<input type="checkbox" name="checkall" onclick="ChangeCheckStatus();"
Later in the form is the code to create the individual checkboxes.
PHP Code:
for ($i = 0; $i < $some_variable; ++$i)
{
  <input type="checkbox" name="checkall_"<?php echo $i?>">
}
I know I will need to do a loop to see them all but I used the following just to see if the basic code is working. But when I click on the main checkbox, nothing is displayed. If I change the alert to alert(elm.length);, it displays the total number of input form elements, as expected. Would someone please point out my mistake or let me know if there is a better way to do this?

HTML Code:
<script type="text/javascript">
function ChangeClickedStatus()
{
 var elm = document.getElementsByTagName("input");
 alert(elm[0].name);
}
</script>
benton is offline  
Reply With Quote
Old 12-30-2008, 04:01 AM   #2 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

I was finally able to figure this one out and thought I would post the result here in case others need such a solution. The argument is the name of the main checkbox.
HTML Code:
function ChangeClickedStatus(boxname)
{
  var ison = document.getElementsByName(boxname)[0].checked; 
  var elm = document.getElementsByTagName("input");

  for (j = 0; j < elm.length; j++) 
  {
    if (elm[j].type == 'checkbox')
    {
      if (elm[j].name.search(boxname + "_") == 0)
      {
         if (ison == true)
           elm[j].checked = true;
         else
           elm[j].checked = false;
      }
    } 
  }     
}
benton is offline  
Reply With Quote
The Following 3 Users Say Thank You to benton For This Useful Post:
CoryMathews (12-30-2008), maZtah (01-10-2009), Wildhoney (01-08-2009)
Old 12-30-2008, 05:04 AM   #3 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

quite a handy function thanks for the post.
CoryMathews is offline  
Reply With Quote
Old 01-08-2009, 07:16 PM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thanks. I'm pleased to be able to give something back to this great site, which has helped me a number of times.
benton is offline  
Reply With Quote
Old 01-08-2009, 11:04 PM   #5 (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

Thanks a lot It's certainly very useful to implement this on any site that has many checkboxes. Also, I like Gmail's approach of holding down shift and clicking the first and last checkbox, and it fills in all the rest between. That's very useful.
__________________
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
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
jQuery and firefox checkboxes?? drewbee Javascript, AJAX, E4X 1 08-27-2008 05:54 PM


All times are GMT. The time now is 06:25 PM.

 
     

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