S
stevewy
I am trying to write a function that will test all the checkboxes in a
particular group of a form (it's a questionnaire), see whether more
than three of them are ticked, and display a message if there are. To
avoid putting an onClick on each individual checkbox, I am trying to
put an onClick in the <TABLE> tag that surrounds each question and use
srcElement to tell which group has been clicked on and check each
checkbox within that group in turn, counting up any ticked ones as I
go. I am having trouble accessing each individual element index
within the group, ie if a batch of checkboxes were all grouped as V6,
the first one would be called V6[0], then V6[1] etc. I thought I could
use a for...next loop to cycle through them. Have a look at what I've
got so far, which doesn't work. Does anyone see where I am going
wrong?
function limitchecked(){
obj=event.srcElement;
if (obj.type=="checkbox"){
var group=form.elements[obj.name] // try to create a group based on
the name of the question
var count=0;
for(var i=0;i<group.length;count+=Number(group[i++].checked)) //
cycle through the group
if(count>3){
alert("You may only check three items.");
event.srcElement.id=false;}}}
Steve Wylie
particular group of a form (it's a questionnaire), see whether more
than three of them are ticked, and display a message if there are. To
avoid putting an onClick on each individual checkbox, I am trying to
put an onClick in the <TABLE> tag that surrounds each question and use
srcElement to tell which group has been clicked on and check each
checkbox within that group in turn, counting up any ticked ones as I
go. I am having trouble accessing each individual element index
within the group, ie if a batch of checkboxes were all grouped as V6,
the first one would be called V6[0], then V6[1] etc. I thought I could
use a for...next loop to cycle through them. Have a look at what I've
got so far, which doesn't work. Does anyone see where I am going
wrong?
function limitchecked(){
obj=event.srcElement;
if (obj.type=="checkbox"){
var group=form.elements[obj.name] // try to create a group based on
the name of the question
var count=0;
for(var i=0;i<group.length;count+=Number(group[i++].checked)) //
cycle through the group
if(count>3){
alert("You may only check three items.");
event.srcElement.id=false;}}}
Steve Wylie