Skip to main content
Gabriel Aparecido
Participant
February 13, 2021
Answered

How to change checkbox color when it is checked?◘

  • February 13, 2021
  • 1 reply
  • 5307 views

I'm creating a exam with multiple choices per question. The idea is to, when the student clicks on one of the chekboxes, he receives a feedback color on that checkbox through colors.

A simple system with  red when wrong and green when right.

 

In the forms options I didn't find nothing like that, and the only alternative I found was to create a JavaScript, but I don't know anything about that.

 

I anexed a example file. The checkboxes are placed, but a just don't know how to gave this system with the colors.

Correct answer Nesa Nurani

You want to change fill color or border color?

You can aproach this a few way, simpliest being just change color (Il use fill color as example) when checkbox is clicked,

since you use checkboxes as radio buttons, you can use this code in first checkbox as Mouse UP event, where "0" in field name represent field widget (In a group first field will have widget 0, second will have 1, then 2,3...etc)

if(this.getField("eqexam.Part1.2.0").value != "Off"){
this.getField("eqexam.Part1.2.0").fillColor = color.red;}

In the second checkbox replace "0" with "1" in third checkbox with "2"...etc

also change "red" to "green" in a checkbox you want to be green.

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 13, 2021

You want to change fill color or border color?

You can aproach this a few way, simpliest being just change color (Il use fill color as example) when checkbox is clicked,

since you use checkboxes as radio buttons, you can use this code in first checkbox as Mouse UP event, where "0" in field name represent field widget (In a group first field will have widget 0, second will have 1, then 2,3...etc)

if(this.getField("eqexam.Part1.2.0").value != "Off"){
this.getField("eqexam.Part1.2.0").fillColor = color.red;}

In the second checkbox replace "0" with "1" in third checkbox with "2"...etc

also change "red" to "green" in a checkbox you want to be green.

Participant
October 22, 2024

Hello. Is there a way to preserve the selection when I flatten the form fields? This code works great when the PDF is editable but when I flatten the form fields all of my checkboxes are colored in. Thanks.

AnandSri
Legend
January 9, 2025

Hi,

 

I hope you are doing well, and apologies for the delayed response.

 

To preserve the selection and prevent the checkboxes from changing color when flattening form fields, you can adjust your approach as follows:

  1. Ensure the checkbox color is set before flattening: Flattening the form fields will turn the interactive form elements (like checkboxes) into static content, which means any color changes made dynamically during form interaction may not persist. You should apply the color change directly to the checkbox appearance before flattening.

  2. Use JavaScript to set the color: Instead of relying on the default behavior, you can write a small script to set the checkbox color. Here's an example of how to do that:

    • Open the PDF in Acrobat.
    • Select the checkbox form field.
    • Go to the "Actions" tab and choose "Run a JavaScript."
    • Use a script like the following to set the color of the checkbox when checked:
    if (this.getField("checkbox1").value == "Yes") {
    this.getField("checkbox1").buttonSetCaption("✔").fillColor = color.red; // Choose your desired color
    }
  3. Flatten with custom settings: After setting the colors with the JavaScript, you can flatten the form fields by choosing "Tools > Prepare Form" and then flatten the form. This will convert the interactive checkboxes to static content, keeping the color as per your script.

By ensuring the colors are set before flattening, and using a JavaScript workaround, you can preserve the selections and avoid unwanted changes to the checkbox colors when flattening the form. Please note that this is a sample code, and it may not work in your specific environment. You might need to adjust it according to your workflow. You can also see this article for more detailed information: https://adobe.ly/40cqBxM

 

Thanks,

Anand Sri.