Link to home
Start Free TrialLog in
Avatar of global_expert_advice
global_expert_adviceFlag for India

asked on

check multiple checkbox checked using array values

hi
below is the code for multiple checkbox, which is working perfectly the way i want.. like if you select doesnt matter other check box gets unchecked... and when you check other checkboxes doesnt matter checkbox gets unchecked... so its like doesnt matter v/s other checkbox... this is just explanation of my code...
Now what new is i want...

I have the edit page.. where i fetch the values from database.. for marital status which is having 4-5 checkbox.. its  values are coming in the form of array... like

Doesnt matter, Never Married, Divorced
So my question is how do i check the respective checkboxes based on the value i fetch from database and matching it with checkbox.. and only problem i have is my values for all checkboxes are coming  ..separated by comma... so how to split and do the task...
below is the current code..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var clearTextWhenOtherSelected = true;
function list(c,n,z)
{
  if(clearTextWhenOtherSelected)
  {
    document.getElementById('data_marital_part').value = '';
    clearTextWhenOtherSelected = false;
  }
  s=document.InstantSMS1.maritalstatusdata.value;
  if(c.checked)
  {
    if(s.indexOf(n)<0) s+=','+n;
  }
  else
  {
    s=document.InstantSMS1.maritalstatusdata.value.replace(','+n,'');
  }
  z=",";

  if (s.substring(2) == z) s=s.substring(2);
    document.InstantSMS1.maritalstatusdata.value=s;
}


function txtrep() {
var newtext = "Second text here";
document.getElementById('data_marital_part').value = newtext;
clearTextWhenOtherSelected = true;

}
</script>
</head>
<body>
<form name="InstantSMS1">
<h1>Marital Status :</h1><br />
<input name="mStatus" type="checkbox" id="doesntmatter" value="Doesn't Matter" onclick="r=document.getElementsByName('mStatus');for(var i=0;i<r.length;i++) if(r[i]!=this)r[i].checked=r[i].checked&&!this.checked;list(this,'Do Not Matter');txtrep()" />Doesn't Matter<br />
<input name="mStatus" type="checkbox" value="Never Married" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Never Married');" />Never Married<br /> 
<input name="mStatus" type="checkbox" value="Divorced" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Divorced');" />Divorced<br /> 
<input name="mStatus" type="checkbox" value="Widowed" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Widowed');" />Widowed<br /> 
<input name="mStatus" type="checkbox" value="Separated" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Separated');" />Separated<br /> 
<input name="mStatus" type="checkbox" value="Annulled" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Annulled');" />Annulled<br /> 
<br />
<br />
<input type="text" name="maritalstatusdata" id="data_marital_part" value="" />
</form>
</body>
</html>

Open in new window


Thanks
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland 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 global_expert_advice

ASKER

hi
radio button is not required... as i want to pass the checkbox values using concatenating to string format... like ,2,3,4,5

also my question is how to make the checkbox checked by matching its value with the string value... it can be matched and done.. if the fetching value is single... like 2 but values are 2,3,4,5
so if checkbox value is 3 ... and if it matched with fetched value... it should be checked...
Avatar of Shinesh Premrajan
Possibility here it depends upon how are you getting the data from the database, I mean the server end and how you are handling it.

I suggest implementing it using the server technology rather then at the client side, a easy approach. I its agreeable then pls mention the server technology you are using.
I've requested that this question be deleted for the following reason:

no specific answer, i solved it myself thanks
hi

my question was realed to javascript function for checking and unchecking with few options, but the expert comments where not specific, infact i provided full coding too but they didnt able to understand and  may i m not able to explain things. Thats the reason i asked to  close this question