Title: Check a Checkbox with Code
Last modified: September 11, 2024

---

# Check a Checkbox with Code

 *  Resolved [eberkland](https://wordpress.org/support/users/eberkland/)
 * (@eberkland)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/)
 * For simplicity’s sake, let say I have a dropdown field (fieldname1) that has 
   two choices; Fruits and Vegetables.
 * Fieldname2 is a checkbox field with choices for different fruits and vegetables;
   bananas, carrots, mushrooms, strawberries, etc.
 * Selecting any of the choices in fieldname2 triggers a dependent div field that
   has a calculator for the price of that item (e.g. clicking on bananas gives you
   the banana calculator)
 * If Fruits is selected in the dropdown in fieldname1, what is the auxiliary code
   to check the boxes for all fruits in fieldname2, but also let the user deselect
   any unwanted choices (e.g. they want to see the calculator for bananas but not
   strawberries.
 * I would appreciate any help. Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009285)
 * Hello [@eberkland](https://wordpress.org/support/users/eberkland/)
 * Assuming you have the fieldname1, with two choices, fruits and vegetables, and
   the checkbox field fieldname2 with the choices bananas, strawberries, carrots,
   and lettuce. If the user selects fruits in fieldname1 you want to tick the bananas,
   strawberries choices in fieldname2, carrots, and lettuce choices if the user 
   selects vegetables.
 * You can insert a calculated field in the form to be used as an auxiliary (you
   can hide it by ticking a checkbox in its settings), and enter the equation:
 *     ```wp-block-code
       getField(fieldname2|n).setVal(IF(fieldname1=='fruits', ['bananas', 'strawberries'], ['carrots', 'lettuce']))
       ```
   
 * Or if you prefer to implement the equation with function structure:
 *     ```wp-block-code
       (function(){    if(fieldname1=='fruits'){        getField(fieldname2|n).setVal(['bananas', 'strawberries']);    } else {        getField(fieldname2|n).setVal(['carrots', 'lettuce']);    }})()
       ```
   
 * Best regards.
 *  Thread Starter [eberkland](https://wordpress.org/support/users/eberkland/)
 * (@eberkland)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009316)
 * Thanks. What if there is an item that is not a fruit or a vegetable as an option
   in fieldname2, like say a pizza or a gallon of milk? Will those remain unchecked
   but remain as a selection option?
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009320)
 * Hello [@eberkland](https://wordpress.org/support/users/eberkland/)
 * The previous implementation ticks the fieldname2 choices based on the fieldname1
   value. Other choices remain visible and selectable. If you need to hide or deactivate
   the unrelated options, you must implement a more complex equation.
 * If you need a custom coding service to implement a custom behavior, like the 
   one described above, you can contact us directly through the plugin website. 
   [Contact Us](https://cff.dwbooster.com/customization).
 * Best regards.
 *  Thread Starter [eberkland](https://wordpress.org/support/users/eberkland/)
 * (@eberkland)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009322)
 * Are we talking JQuery using .prop(‘checked’,true) similar to this question; [https://wordpress.org/support/topic/jquery-checkbox-propchecked-true/](https://wordpress.org/support/topic/jquery-checkbox-propchecked-true/)
 *  Thread Starter [eberkland](https://wordpress.org/support/users/eberkland/)
 * (@eberkland)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009323)
 * Disregard last question. If everything else stays and is selectable. My problem
   is solved. You can mark as resolved. Thanks.
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009324)
 * Hello [@eberkland](https://wordpress.org/support/users/eberkland/)
 * Not exactly. The code `.prop('checked',true)` does not trigger the onchange events
   required to evaluate the dependencies and the equations. For this reason, the
   code in the other support thread must call the onchange event directly. The `
   setVal` method does both operations. It ticks the corresponding choices and triggers
   an onchange event at the end of the process.
 * However, it does not disable the other choices in the checkbox field.
 * Best regards.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Check a Checkbox with Code’ is closed to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [codepeople](https://wordpress.org/support/users/codepeople/)
 * Last activity: [1 year, 9 months ago](https://wordpress.org/support/topic/check-a-checkbox-with-code/#post-18009324)
 * Status: resolved