PDA

View Full Version : How to loop an array?


buckbeak
09-13-2002, 05:03 AM
I have a list of parts with checkbox beside them. So when user click on the checkbox, the value of the checkbox will pass to a another textbox.

The problem is there are multiple checkbox. Here are my codes:-
I'm only able to pass only one value.

<script language="javascript">
<!--
var frm = document.list;
function Update(fld){
document.list.post_here.value = fld;
</script>

<input type="checkbox" name="check_stk" value="part1" onClick="javascript:Update(this.value)">Part 1
:
Part 2 and so on.......

<input type="text" name="post_here" value="">

If more then one check box is selected, the value on textbox post_here suppose to be part1, part2,part3, etc......

I thought of putting it in array, but i dunno how to loop an array!

Please help!

x_goose_x
09-13-2002, 05:16 AM
To loop through an array you would use "FOR"

myarr = newArray();
for (x=0; x<myarr.length; x++) {
myarr[x].value="whatever"
}

joh6nn
09-13-2002, 06:31 AM
or, simultaneously simpler, and more complex:

myarr = newArray();
for ( var x in myarr ) {
myarr[x].value="whatever"
}

buckbeak
09-13-2002, 07:22 AM
I'm really lousy at javascript....

How do i put the fld value in the loop?

joh6nn
09-13-2002, 07:39 AM
sorry, didn't read your post closely enough.

try the following:

<script>
function() {
document.list.post_here.value = "" ;
var checkBoxes = document.list.check_stk;
for ( var i = 0; i < checkBoxes.length; i++) {
if ( checkBoxes[i].checked ) {
document.list.post_here.value += checkBoxes[i].value + ',';
}
}
}
</script>

<input type="checkbox" name="check_stk" value="part1" onClick="Update()">Part 1

i have the feeling that this solution is bulky and inelegant, but i seriously wasn't able to come up with a better one quickly. if something occurs to me later, i'll be sure to post it.