Copy link to clipboard
Copied
Hey,
Apologies in advance - I'm an old dog trying to learn new tricks.
I've created a questionnaire using Prepare form, and there are 17 questions, each with a pair of Radio Buttons. I've tried to add a javascript to each one that turns the 'No' option to red if it's selected. I've managed that, but then when any subsequent options are selected, the red disappears and it just returns to transparent. Does anyone know how I can change my code to keep it red? Here's the code I plagiarised from another forum...
// Loop through questions q1 to q17 and apply the color change
for (var i = 1; i <= 17; i++) {
// Define the current radio button group name
var fieldName = "q" + i;
// Get the field object
var field = this.getField(fieldName);
// Check if Choice2 is selected
if (field.value === "Choice2") {
// Change text color of Choice2 to red
field.textColor = color.red;
}
}
Can anyone help? I just have no idea where to start.
Thank you so much!
This code edits all the fields, though, not just the one you pasted it under. You need to either modify it to edit a single field at a time (you can get access to it via event.target), or place a single copy of it under the Calculation event of some text field.
Also, it will not work if your fields are named "Q1", "Q2", as it references fields called "q1", "q2", etc., and JS is case-sensitive.
Copy link to clipboard
Copied
Where did you place this code? What triggers it?
Also, you should add an else-clause to change the field's color back to Black (or whatever other color) if "Choice2" is not ticked.
Copy link to clipboard
Copied
Sorry, I should've said that...
Under 'General' I have each group named Q1, Q2, Q3 etc. And then in 'Radio Button Choice' under 'Options', I have 'Choice1' and 'Choice2' for each group.
Then under 'Actions', I added a Mouse Up action to run JavaScript, and pasted this code for each individual 'Choice2'
Copy link to clipboard
Copied
This code edits all the fields, though, not just the one you pasted it under. You need to either modify it to edit a single field at a time (you can get access to it via event.target), or place a single copy of it under the Calculation event of some text field.
Also, it will not work if your fields are named "Q1", "Q2", as it references fields called "q1", "q2", etc., and JS is case-sensitive.
Copy link to clipboard
Copied
Thank you for your help! It works perfectly now. Amazing, thank you!