Click to See Complete Forum and Search --> : Help please?
rcamp
03-28-2003, 08:05 AM
I have 17 checkboxes- I need to check which ones have been checked (max 3 - I have a function that checks the max amount already) the thing I need help with is assigning the three checked boxes to three variables say 1, 2 and 3. I want this to be within its own function - I will make the variables global so I can access them from other functions (my insert function).
My checkboxes are set up as follow:
<td><input type='checkbox' name='reason1' value='1' onClick="countChoices(this)">Internal rattling</td>
Can anyone help please :)
I think your best bet would be for all of the checkboxes to have the same name, e.g. "response".
Then, you could loop though to find the selected checkboxes using code such as:
var index = 0;
var answers = new Array();
for (var i=0; i < response.length; i++) {
if (response[i].checked) {
answers[index] = response.value;
index++;
}
}
Obviously, the value of each of your text boxes will have to equal the literal string you want each one to represent.
Hope that helps...
havik
03-28-2003, 10:33 AM
Well, here is the function from the check boxes thread that I wrote for you.
function countChoices(field) {
count = 0;
max = 3; // max. number allowed at a time
boxes = new Array();
for(i = 0; i < document.frmHandsetRepair.reasons.length; i++){
boxes[i] = document.frmHandsetRepair.reasons[i].checked;
count += (boxes[i] ? 1 : 0);
}
if (count > max) {
alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick another.");
field.checked = false;
}
}
If you want the values, do the following:
function getValues(){
var index = 0;
var max=3;
var values = new Array();
for (var i=0; i < document.frmHandsetRepair.reasons.length; i++) {
if (document.frmHandsetRepair.reasons[i].checked) {
values[index] = document.frmHandsetRepair.reasons[i].value;
index++;
}
}
and call this function within the countChoices function here:
if(count > max) {
getValues();
}
after this, your values array should contain the values you want.
Havik
khalidali63
03-28-2003, 10:33 AM
First of all you should not have started a new thread for the problem which is being discussed already in th eforum.
http://forums.webdeveloper.com/showthread.php?s=&threadid=6738
Here try this link..and see if this is ehat you want to get done.
http://68.145.35.86/temp/rcamp-CheckBoxes.html
Cheers
Khalid
havik
03-28-2003, 10:35 AM
exactly, that's what I was mentioning.
Havik