Link to home
Start Free TrialLog in
Avatar of quanghoc
quanghoc

asked on

get all checkbox status in a form

I don't have much time to research about Javascript. However, I have a good knowledge about programming generally. Here is the description of my problem:
+ Given a form name 'myform'
+ Given multiple checkbox with different id name
+ Perform a loop to scan through all available checkboxes in the form and see if the checkbox is check, then get the value in put into a string 'str'. The seperator string is "|||". If the checkbox is uncheck, then do nothing.
The things that I don't know is how to scan through the form and "know" all the checkboxes and get the value/status of it. Please help. Thanks.
Avatar of svd2002
svd2002

<SCRIPT LANGUAGE=javascript>
<!--
function fnTest()
{
 var lmax = document.forms[0].elements.length;
 var lCounter;
 var element;
 var str = "" ;
 for (lCounter=0; lCounter < lmax; lCounter++)
 {
  element = document.forms[0].elements[lCounter];
  if (element.type == "checkbox")
  {
   if (element.checked)
   {
    if (str == "")
    {
     str = "true";
    }    
    else
    {
      str = str + "|||true";
    }    
   }    
  }    
}
 return str;
     }
//-->
</SCRIPT>
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of quanghoc

ASKER

This was perfect. Thank you.