Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Checking some checkboxes via their name in a HTML form

Hello All,

I have a  HTML form set up with around 20 - 25 checkboxes (variable number) called "cmbShow1" , "cmbShow2" ... "cmbShowX"

Where X is a variable number and can change depending on what page I am on.

Is there a way to simple check all checkboxes that are named "CmbShow<what ever>" on a page.

I cannot use a "check all" function because I have other check boxes on the page that need to be left alone.

Thanks,

Nach24.
0
Nach24
Asked:
Nach24
1 Solution
 
ZylochCommented:
Hi Nach24,

var myForm = document.forms[0];
var i=1;
while (myForm.elements["cmbShow"+i] != undefined) {
   //Check for myForm.elements["cmdShow"+i]
   i++;
}

Regards,
Zyloch
0
 
str_kaniCommented:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function cmb_show(n)
{
for(i=1;i<=n;i++)
      document.f1.elements["cmbShow"+i].checked=true;
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<form name="f1">
<INPUT TYPE="checkbox" NAME="cmbShow1">
<INPUT TYPE="checkbox" NAME="cmbShow2">
<INPUT TYPE="checkbox" NAME="cmbShow3">
<INPUT TYPE="checkbox" NAME="cmbShow4">
<INPUT TYPE="checkbox" NAME="cmbShow5">
<INPUT TYPE="button" name="btn1" value="check all acmshow" onclick="cmb_show(4)">
</form>
</BODY>
</HTML>
0
 
str_kaniCommented:
In this example,
I have created 5 check boxes, and passed only 4 to the function, so it checks upto 4, if you want to check only 2 pass 2 if you want to check all 5 pass 5.

Hope this helps.
0
 
prsupriyaCommented:
Try this...

<script language="JavaScript">
      function callme(){
      count = document.forms[0].elements.length;
       for (var i = 0; i <count; i++) {
          var dchk="cmbShow"+(i+1);
          if ((document.forms[0].elements[i].name == dchk) && (document.forms[0].elements[i].type == 'checkbox')) {
                  document.forms[0].elements[i].checked = 1;
          }
        }
      }
</script>

<body onload="callme()">
<form name="frm1" method="post">
<input type="Checkbox" name="cmbShow1" value="">
<input type="Checkbox" name="cmbShow2" value="">
<input type="Checkbox" name="cmbShow3" value="">
<input type="Checkbox" name="1cmbShow2" value="">
</form>
</body>
0
 
thirdCommented:
"Where X is a variable number and can change depending on what page I am on."

so this doesn't mean it will start form one (1).

try,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
  function toggleCheckAll(obj, flag){
    for(var i=0; i<obj.elements.length; i++){
        if(obj.elements[i].type=='checkbox' && obj.elements[i].name.indexOf('cmbShow')==0){
          obj.elements[i].checked = flag;
        }
      }
  }
</script>
</head>
<body>
<form name="form1" method=post action="" onsubmit="">
<input type="checkbox" name="cmbShow23">1<br>
<input type="checkbox" name="cmbShow33">2<br>
<input type="checkbox" name="cmbShow43">3<br><br>
<input type="checkbox" name="cmbOthers">Others<br><br>
<input type="button" value="Check All cmbShow" onclick="toggleCheckAll(this.form, true);">
<input type="button" value="Uncheck All cmbShow" onclick="toggleCheckAll(this.form, false);">
</form>
</body>
</html>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now