473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to check and unselect all with single checkbox?

32
hi i have created function that automatically adds rows with chekcboxes. i can create two button that one serves checking all checkboxes and the another one serves for unchecking rows. but i would like to check/uncheck all checkboxes with single checkbox.
then i have created checkbox for the purpose to check or uncheck all checkboxes. but it neigther checks nor unchecks all
here is script
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <script>
  7. var a=0;
  8. function add(){
  9.     a++;
  10.  
  11.  
  12.     var cedvel=document.getElementById("tab");
  13.  
  14.  
  15.     if(a>cedvel.rows.length){
  16.  
  17.         a=cedvel.rows.length;
  18.  
  19.         }
  20.  
  21.  
  22.  
  23.     var sira=cedvel.insertRow(a);
  24.  
  25.     var isare=sira.insertCell(0);
  26.     var solteref=sira.insertCell(1);
  27.     var sagteref=sira.insertCell(2);
  28.  
  29.     var birinci=document.createElement('input');
  30.     birinci.type='checkbox';
  31.     isare.appendChild(birinci);
  32.  
  33.     solteref.innerHTML=a+1;
  34.  
  35.     var ucuncu=document.createElement('input');
  36.     ucuncu.type="text";
  37.     ucuncu.size="11";
  38.     sagteref.appendChild(ucuncu);
  39.  
  40.     }
  41.  
  42. function del(){
  43.     var cedvel=document.getElementById("tab");
  44.     var a=cedvel.rows.length;
  45.     for(i=0; i<a; i++){
  46.  
  47.         var komp=cedvel.rows[i].cells[0].childNodes[0];
  48.         if(komp.checked==true){
  49.  
  50.             cedvel.deleteRow(i);
  51.             a--;
  52.             i--;
  53.  
  54.             }
  55.         if(i>=0)
  56.         cedvel.rows[i].cells[1].childNodes[0].nodeValue=i+1
  57.         }
  58.  
  59. }
  60.  
  61. function checkall(){
  62.     var sec=document.getElementById("tab");
  63.     for(i=0; i<sec.rows.length; i++){
  64.  
  65.         sec.rows[i].cells[0].childNodes[0].checked=true;
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.     }
  72.  
  73. function uncheckall(){
  74. var sec=document.getElementById("tab");
  75.     for(i=0; i<sec.rows.length; i++){
  76.  
  77.         sec.rows[i].cells[0].childNodes[0].checked=false;
  78.  
  79.         }
  80.  
  81. }
  82. var checked=1;
  83. function all(){
  84.     var sec=document.getElementById("tab");
  85.     for(i=0; i<sec.rows.length; i++){
  86.         if(checked==1){
  87.         sec.rows[i].cells[0].childNodes[0].checked=true;
  88.         checked=2;
  89.         }
  90.         if(checked==2){
  91.         sec.rows[i].cells[0].childNodes[0].checked=false;
  92.         checked=1;    
  93.  
  94.         }
  95.         }
  96.  
  97.  
  98.  
  99.     }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. </script>
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. </head>
  114.  
  115. <body bgcolor="#999999">
  116. &nbsp<input type="checkbox" onclick="all()"  value="check/uncheckall" id="box"/>check/uncheck all
  117. <table  id="tab" style="font-family:'Comic Sans MS', cursive">
  118. <tr><td><input type="checkbox" /></td><td>1</td><td><input type="text" size="11" /></td></tr>
  119. </table>
  120. <input type="button" value="add" onclick="add()"  style="font-family:'Comic Sans MS', cursive"/><br />
  121. <input type="button" value="delete" onclick="del()" style="font-family:'Comic Sans MS', cursive"/>
  122. <input type="button" value="uncheckall" onclick="uncheckall()" style="font-family:'Comic Sans MS', cursive" /><br />
  123. <input type="button" value="checkall" onclick="checkall()" style="font-family:'Comic Sans MS', cursive" /><br />
  124.  
  125.  
  126. </body>
  127. </html>
Thanks in advance for attention
Feb 2 '11 #1
2 2038
RamananKalirajan
608 512MB
Hi azegurb,
I went through your code. Some small changes will make ur code work.

In HTML

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" onclick="all1(this)"  value="check/uncheckall" id="box"/>
please change the function name and add a parameter. I doubt there is some predefined function all() in JS. Because when I tried with all() it was not working.

In JS,

Expand|Select|Wrap|Line Numbers
  1. function all1(ths){
  2.   if(ths.checked==true)
  3.      checkall();
  4.   else
  5.     uncheckall();
  6. }
Thanks and Regards
Ramanan Kalirajan
Feb 8 '11 #2
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="checkall" id="checkall" onclick="check_all()">Select All
  2. <br>
  3.  
  4. <input type="checkbox" name="check_0" id="check_0">Option 1
  5. <input type="checkbox" name="check_1" id="check_1">Option 2
  6. <input type="checkbox" name="check_2" id="check_2">Option 3
  7. <input type="checkbox" name="check_3" id="check_3">Option 4
  8. <input type="checkbox" name="check_4" id="check_4">Option 5
  9. <input type="checkbox" name="check_5" id="check_5">Option 6
  10. <input type="checkbox" name="check_6" id="check_6">Option 7
  11. <input type="checkbox" name="check_7" id="check_7">Option 8
  12. <input type="checkbox" name="check_8" id="check_8">Option 9
  13. <input type="checkbox" name="check_9" id="check_9">Option 10
  14.  
  15. <br><hr>
  16.  
  17. <script type="text/javascript">
  18. function check_all()
  19. {
  20.  
  21.     for(i=0;i<10;i++)
  22.     {
  23.         tmp_checkbox_id = "check_"+i;
  24.             if(document.getElementById("checkall").checked == true)
  25.             {    
  26.                                 document.getElementById(tmp_checkbox_id).checked = true;
  27.             }
  28.             else
  29.             {    
  30.                 document.getElementById(tmp_checkbox_id).checked = false;
  31.             }
  32.  
  33.     }
  34. }
  35. </script>
May 16 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
0
by: vpcs | last post by:
I am trying to do an invoicing software with perl - cgi. I am having a problem in associating the checkbox with the items from the database. I am giving the user option to enter the customer...
3
by: micmic | last post by:
Deear all experts, In the MySQL, we can use escape character '\' to save the STRING WITH single quote into database (eg. we would like to insert into table "tbl_ABC"with the string ab'c, we...
1
by: SStory | last post by:
How do you guys deal with single quotes with ASP.NET and SQL Server. I tried replacing all ' with two ' before inserting, but that shows up as two single qutoes. if I don't do that I get a...
3
by: Mark | last post by:
Hi everyone, could someone please recommend a way to do the following. I have a datagrid which contains a bound template column consisting of checkbox and a string pulled from a database table....
2
by: Kevin | last post by:
I've been looking all over and I can't seem to find what ought to be simple. I need to disable a drop down when a checkbox is checked, and enable it when same checkbox is unchecked. I've...
1
by: Eric Layman | last post by:
Hi, I have a form with checkboxes. if i do a alert(form.testid.length), it will echo me the number of chexkboxes But if there is only a single checkbox, i wouldnt be able to retrieve the...
7
by: ggfota | last post by:
I have problem creating Access (Continuous) Form with indepenent CheckBox for each record. The user should be able to select records that will be processed by VBA based on ChceckBox value. When I...
1
by: vivek kapile | last post by:
Title:Dynamically adding table row with a checkbox using JavaScript Author:Vivek Kapile Email:snipped Language:JavaScript Platform:JavaScript in ASP.net Technology:Used in ASP.net...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.