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

Question

Question

How to replace radio options at index in new forms?

asked on April 22

I wanted to make a dynamic set of choices using LFForm.changeFieldOptions. I was able to make replace work when I wouldn't specify an index, however that just replaces the entire list of options with one choice. I want to be able to change specific choices using the replace method. Am I using replace wrong?

LFForm.changeFieldOptions( { fieldId: 91 }, [{label: "New Label"}], "replace" );
LFForm.changeFieldOptions( { fieldId: 93, index: 1 }, [{label: "New Label"}], "replace" );

 

LaserficheReplaceShowcase.PNG
0 0

Answer

SELECTED ANSWER
replied on April 26

Index comes into play when you have multiples of the same field (such as within a table or collection) so that isn't going to matter here when trying to target a specific option in a checkbox field.

The issue is that replace is replacing the entire list of options, not specific ones, that's just how it works.  Typically working with this, you'll have an array of the options as a variable in your Javascript and you can modify that array variable and push it out to the field.

Here's an example of some code that updates a checkbox field to some preset labels and values and then changes one of the labels when the field is changed. 

//When the form is loaded, create the array
//for the checkbox options and populate the checkbox.
var checkboxOptions = 
    [
      {label: "Choice 1", value: "One"}, 
      {label: "Choice 2", value: "Two"}, 
      {label: "Choice 3", value: "Three"}
    ];
LFForm.changeFieldOptions( { fieldId: 2 }, checkboxOptions, "replace" );

//When the checkbox is changed, update the label
//on the checkbox item with value of Two.
LFForm.onFieldChange( function() {
  	for (let object of checkboxOptions) {
    	if (object.value == "Two") {
        	object.label = "New Option for Choice 2";
    	}
	}
	LFForm.changeFieldOptions( { fieldId: 2 }, checkboxOptions, "replace" );
}, { fieldId: 2 } );

 

Note that I have this updating both labels and values, because if you only update labels, it will create the values based on the labels, and so any selections from before the change are likely lost when the values change.  But if you include the values in what you are updating in the script, then any selections in those values remain selected through the changes.

2 0
replied on April 29

Thank you so much for the response, I now have a much better understanding of the function! 

1 0

Replies

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

Sign in to reply to this post.