Hey Ya'll

I have a bit of a problem with a script im writing and was wondering if someone could help me out.

I have a number of checkboxes that are dynamically created using the following:

<input type="checkbox" name="we_cater_to[]" value="<?php echo $rowresult['value'];?>" <?php if($plan == 'Free') {?> onclick="chkcontrol(<?php echo $control; ?>)" <?php } else {} ?>>

I also have a javascript function that limits the amount of checkboxes clickable to 3 which is as follows:

<script type="text/javascript">
function chkcontrol(j) {
var total=0;
for(var i=0; i < document.createlisting.we_cater_to.length; i++){
if(document.createlisting.we_cater_to[i].checked){
total =total +1;}
if(total > 3){
alert("Please Select only three")
document.createlisting.we_cater_to[j].checked = false ;
return false;
}
}
} </script>

The problem im having is with the name of the checkboxes "we_cater_to[]" ... If i remove the [] at the end it all works perfect, except that the checkboxes dont save in the way i want them to ... so i need the [] to make the array ... and if i add [] to the javascript function then the function doesnt work at all.

How do i get the javascript to work using the [] ?

Any help would be great, i have been racking my brains and cant seem to get a result.

Cheers,

    you might want to try document.getElementsByTagName("input") which will grab more than just the we_cater_to inputs but you can check the properties of the inputs returned to see if their name starts with we_cater_to.

      Is there any chance you could give me an example ... Im not too clued up on JavaScipt

      Cheers,

        <script type="text/javascript">
        function report_inputs() {
        	var x = document.getElementsByTagName('input');
        	var str = '';
        	for (var i=0; i<x.length; i++) {
        		str += x[i].name + " is of type " + x[i].getAttribute('type') + "\n";
        	}
        	alert(str);
        }
        </script>
        

          Cheers for that ... Should i replace my javascript function with the one listed above and change the onclick to report_inputs() ?

            Write a Reply...