Checking some checkboxes via their name in a HTML form

Posted on 2005-05-12
Last Modified: 2008-03-10
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.


Question by:Nach24
    LVL 36

    Expert Comment

    Hi Nach24,

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

    LVL 12

    Accepted Solution


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <TITLE> New Document </TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function cmb_show(n)

    <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)">
    LVL 12

    Expert Comment

    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.
    LVL 5

    Expert Comment

    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;

    <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="">
    LVL 30

    Expert Comment

    "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).


    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
      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;
    <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);">

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Suggested Solutions

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now