Copy link to clipboard
Copied
Hi there!
I'm trying to insert some javascript into my form that limits the number of radio buttons. I have a total of 8 buttons that can be selected but I want to restrict only 3 or less to be selected (not all 8). I have inserted the javascript into the document – however something is going terribly wrong.
I have named each radio button "service". And added the following document level javascript:
function limitChoices() {
// Get the value of the radio button
var v = getField("service").value;
// Set the maximum number of check boxes that can be selected
var max_cbs = v == 3;
// Initialize the selected check box count
var count = 0;
// Loop through the check boxes to see if the limit has been exceeded
for (var i = 0; i < 8; i += 1) {
// Increment the counter if the current check box is selected
if (getField("box." + i).value !== "Off") count += 1;
// If the count exceeds the limit, deselect the check box that was just selected
// and alert the user
if (count > max_cbs) {
event.target.value = "Off";
app.alert("Only " + max_cbs + " colour(s) available for this option.", 3);
break;
}
}
}
I have also added
limitChoices();
to each mouseover state of the radio button.
I have no real knowledge of javascript, so I realise the code I am using is probably wrong! Any help would be greatly appreciated
It's not a good idea to use radio-buttons for this, as they can't be "un-selected". You should use check-boxes instead.
Copy link to clipboard
Copied
It's not a good idea to use radio-buttons for this, as they can't be "un-selected". You should use check-boxes instead.
Copy link to clipboard
Copied
Thank you! That's where I was going wrong All solved and working!
Copy link to clipboard
Copied
Hello, I have to create a vote among 25 buttons or check marks. People can only select 20 answers
how to create this limit of answers?
Script or? attached the pdf, we must select only 20 yes at most, thank you .