threezero
06-01-2003, 04:03 PM
Hi
First off, I am quite new to Javascript so please bear with me. I may not be going about this in the right way, so any advice would be appreciated.
I have included some test code I am working on below but here's a quick summary of what i am trying to achieve...
I will have a set of 31 checkboxes, which the site visitor will check dependent on their respective choices. I want to store the choices in an array and if the visitor chooses checkbox 25 then a value is added to position 25 in the array. As the visitor selects/deselects various checkboxes the array is dynamically updated in length and value.
When the visitor has finished the array may have empty values and the first and last value of the array may or may not be empty.
I wish to detect the first and last 'checked' value in the array.
ie If the first checkbox selected is checkbox 4, then I want to know the value of that checkbox. Same with the last.
Here's the sample code using 4 checkboxes. The values are arbitrary. The textfields are there simply for me to see what is happening as the checkboxes are selected/deselected.
As the code stands I am getting the first value (only if the first checkbox is selected) and the length of the array rather than the last value.
I guess I need to loop through the array again but I am unsure what to look for within the loop.
Can anyone help please...??
TIA
<html>
<head>
<title>Checkbox test</title>
<script language="javascript">
function checkscript() {
var startDate;
var finishDate;
boxes = new Array();
for(i = 0; i < document.test.chk.length; i++){
if (document.test.chk[i].checked==true){
boxes[i] = document.test.chk[i].value;
}
}
startDate = boxes[0];
finishDate = boxes.length;
document.test.dates.value = boxes;
document.test.startDate.value = startDate;
document.test.finishDate.value = finishDate;
}
</script>
</head>
<body>
<form name="test">
1. <input type="checkbox" onClick="return checkscript()" value="DAY1" name="chk"><br>
2. <input type="checkbox" onClick="return checkscript()" value="DAY2" name="chk"><br>
3. <input type="checkbox" onClick="return checkscript()" value="DAY3" name="chk"><br>
4. <input type="checkbox" onClick="return checkscript()" value="DAY4" name="chk"><br>
<br>
Array (boxes): <input type="text" name="dates" size="10"><br>
First Array Value: <input type="text" name="startDate" size="10"><br>
Last Array Value: <input type="text" name="finishDate" size="10"><br>
</form>
</body>
</html>
First off, I am quite new to Javascript so please bear with me. I may not be going about this in the right way, so any advice would be appreciated.
I have included some test code I am working on below but here's a quick summary of what i am trying to achieve...
I will have a set of 31 checkboxes, which the site visitor will check dependent on their respective choices. I want to store the choices in an array and if the visitor chooses checkbox 25 then a value is added to position 25 in the array. As the visitor selects/deselects various checkboxes the array is dynamically updated in length and value.
When the visitor has finished the array may have empty values and the first and last value of the array may or may not be empty.
I wish to detect the first and last 'checked' value in the array.
ie If the first checkbox selected is checkbox 4, then I want to know the value of that checkbox. Same with the last.
Here's the sample code using 4 checkboxes. The values are arbitrary. The textfields are there simply for me to see what is happening as the checkboxes are selected/deselected.
As the code stands I am getting the first value (only if the first checkbox is selected) and the length of the array rather than the last value.
I guess I need to loop through the array again but I am unsure what to look for within the loop.
Can anyone help please...??
TIA
<html>
<head>
<title>Checkbox test</title>
<script language="javascript">
function checkscript() {
var startDate;
var finishDate;
boxes = new Array();
for(i = 0; i < document.test.chk.length; i++){
if (document.test.chk[i].checked==true){
boxes[i] = document.test.chk[i].value;
}
}
startDate = boxes[0];
finishDate = boxes.length;
document.test.dates.value = boxes;
document.test.startDate.value = startDate;
document.test.finishDate.value = finishDate;
}
</script>
</head>
<body>
<form name="test">
1. <input type="checkbox" onClick="return checkscript()" value="DAY1" name="chk"><br>
2. <input type="checkbox" onClick="return checkscript()" value="DAY2" name="chk"><br>
3. <input type="checkbox" onClick="return checkscript()" value="DAY3" name="chk"><br>
4. <input type="checkbox" onClick="return checkscript()" value="DAY4" name="chk"><br>
<br>
Array (boxes): <input type="text" name="dates" size="10"><br>
First Array Value: <input type="text" name="startDate" size="10"><br>
Last Array Value: <input type="text" name="finishDate" size="10"><br>
</form>
</body>
</html>