You are viewing limited content. For full access, please sign in.

Question

Question

JavaScript If then statement

asked on November 4 Show version history

Hi, I have two fields. 1. Yes or No Radio button and 2. Yes or No Checkbox. when I select Yes in the radio button field then I would like to have Yes in the Checkbox selected automatically. However i would like to have this done with a JavaScript. Unless there is an easier way. Anyway, here is my code. Can someone see why this isn't working?

LFForm.onFieldChange(function() {
    let radio = LFForm.getFieldValues({ fieldId: 1 });
    console.log(radio);

    if (radio === "Yes") {
        LFForm.setFieldValues({ fieldId: 2 }, { value: "Yes" });
    }
}, { fieldId: 1 });

 

0 0

Replies

replied on November 5

Super close just two little things that are tripping you up.

  1. LFForm.getFieldValues() when targeting a radio or checkbox returns an object containing a value property.  The value property holds the string representing the selected radio button or checkbox value.
  2. LFForm.setFieldValues() when targeting a radio or checkbox expects the values in array format rather than directly as a string.

 

LFForm.onFieldChange(function() {
    let radio = LFForm.getFieldValues({ fieldId: 1 });
    console.log(radio);

    if (radio.value === "Yes") {
        LFForm.setFieldValues({ fieldId: 2 }, { value: ["Yes"] });
    }
}, { fieldId: 1 });

The key differences from your code are in the if statement checking if the string value of "Yes" is equal to the radio.value rather than the radio object, and the hard brackets around the "Yes" when setting the field values for fieldID 2.

Hope that helps,

1 0
replied on November 5 Show version history

Sweet that worked. So close yet so far!!!

 

Thanks a Million!!!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.