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

Question

Question

Javascript array otherchoicevalue

asked on February 5

Hello,

Juilding javascript to pull hidden fields filled via lookup rules and filling out a form.  I've got the fields used to successfully build an array and fill out checkboxes, including the "_other" checkbox but can't get the value in the text box to fill out.  Any help would be appreciated!  Code attached that works, but my attempts to get the otherChoiceValue syntax has failed

      const checkBoxArray = [];

  if (LFForm.getFieldValues({fieldId:41}) == "True") {
    checkBoxArray.push("choice1");
  }
 if (LFForm.getFieldValues({fieldId:42}) == "True") {
    checkBoxArray.push("choice2");
  }
  if (LFForm.getFieldValues({fieldId:43}) == "True") {
    checkBoxArray.push("choice3");
  }
  if (LFForm.getFieldValues({fieldId:44}) == "True") {
    checkBoxArray.push("choice4");
  }
  if (LFForm.getFieldValues({fieldId:45}) == "True") {
    checkBoxArray.push("choice5");
  }
  if (LFForm.getFieldValues({fieldId:46}) == "True") {
    checkBoxArray.push("choice6");
  }
   if (LFForm.getFieldValues({fieldId:47}) == "True") {
    checkBoxArray.push("_other");//This is where i need the otherchoicevalue
  }

 

0 0

Answer

SELECTED ANSWER
replied on February 5

You do not really even need the if else.  Just create a variable for the otherChoiceValue and set it to blank and then if your field for Other is True, you both push the _other into the array and fill the otherChoiceValue variable with the value it needs.

  const checkBoxArray = [];
  let valueOtherChoice = '';
  if (LFForm.getFieldValues({fieldId:41}) == "True") {
    checkBoxArray.push('Choice_1');
  }
  if (LFForm.getFieldValues({fieldId:42}) == "True") {
    checkBoxArray.push('Choice_2');
  }
  if (LFForm.getFieldValues({fieldId:43}) == "True") {
    checkBoxArray.push('Choice_3');
  }
  if (LFForm.getFieldValues({fieldId:44}) == "True") {
   checkBoxArray.push('Choice_4');
  }
  if (LFForm.getFieldValues({fieldId:45}) == "True") {
    checkBoxArray.push('Choice_5');
  }
  if (LFForm.getFieldValues({fieldId:46}) == "True") {
    checkBoxArray.push('Choice_6');
  }
  if (LFForm.getFieldValues({fieldId:47}) == "True") {
    checkBoxArray.push('_other');
    valueOtherChoice = 'Hello';
  }
  console.log(checkBoxArray);
  console.log(valueOtherChoice);
  LFForm.setFieldValues({fieldId: 1}, {value: checkBoxArray, otherChoiceValue: valueOtherChoice});

 

1 0

Replies

replied on February 5 Show version history

This should give you the answer.  Its with radio buttons but the "Other" option works the same: Modern Form designer javascript "Other" radio button - Laserfiche Answers

Checkbox fields expect an object with a "value" property. For example: {value: ["Choice_1", "Choice_3"]}, {value: ["Choice_1", "_other"], otherChoiceValue: "Hello"}

 

0 0
replied on February 5 Show version history

Yep I've got it working fine with radio buttons, but the syntax is different with checkboxes apparently.  I'm struggling to get it to append as the other choice value must be outside the square brackets and its not taking it with any manipulation I try.

0 0
replied on February 5

I created an example of yours and this script worked to grab the value of the field you want to place in the "Other" box.  Just adjust based on your fields.

// Function to update checkboxes based on field values
function updateCheckboxes() {
    const checkBoxArray = [];

    // Check the value of field 9
    if (LFForm.getFieldValues({fieldId: 9}) == "True") {
        checkBoxArray.push("Choice_1");
    }

    // Check the value of field 10 and add other choice value
    if (LFForm.getFieldValues({fieldId: 10}) == "True") {
        checkBoxArray.push("_other");
        var otherValue = LFForm.getFieldValues({fieldId: 11});
        LFForm.setFieldValues({fieldId: 8}, {value: checkBoxArray, otherChoiceValue: otherValue});
    } else {
        LFForm.setFieldValues({fieldId: 8}, {value: checkBoxArray});
    }
}

// Attach the function to the onFieldChange event of fields 9, 10, and 11
LFForm.onFieldChange(function(event) {
    updateCheckboxes(); // Call the update function
}, { fieldId: 9 });

LFForm.onFieldChange(function(event) {
    updateCheckboxes(); // Call the update function
}, { fieldId: 10 });

LFForm.onFieldChange(function(event) {
    updateCheckboxes(); // Call the update function
}, { fieldId: 11 });

 

1 0
SELECTED ANSWER
replied on February 5

You do not really even need the if else.  Just create a variable for the otherChoiceValue and set it to blank and then if your field for Other is True, you both push the _other into the array and fill the otherChoiceValue variable with the value it needs.

  const checkBoxArray = [];
  let valueOtherChoice = '';
  if (LFForm.getFieldValues({fieldId:41}) == "True") {
    checkBoxArray.push('Choice_1');
  }
  if (LFForm.getFieldValues({fieldId:42}) == "True") {
    checkBoxArray.push('Choice_2');
  }
  if (LFForm.getFieldValues({fieldId:43}) == "True") {
    checkBoxArray.push('Choice_3');
  }
  if (LFForm.getFieldValues({fieldId:44}) == "True") {
   checkBoxArray.push('Choice_4');
  }
  if (LFForm.getFieldValues({fieldId:45}) == "True") {
    checkBoxArray.push('Choice_5');
  }
  if (LFForm.getFieldValues({fieldId:46}) == "True") {
    checkBoxArray.push('Choice_6');
  }
  if (LFForm.getFieldValues({fieldId:47}) == "True") {
    checkBoxArray.push('_other');
    valueOtherChoice = 'Hello';
  }
  console.log(checkBoxArray);
  console.log(valueOtherChoice);
  LFForm.setFieldValues({fieldId: 1}, {value: checkBoxArray, otherChoiceValue: valueOtherChoice});

 

1 0
replied on February 10

Y'all are amazing.  Thanks for the help with this, got it going following your examples

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

Sign in to reply to this post.