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

Question

Question

Forms 11 Update 3 - using checkbox for onFieldChange

asked on August 11, 2023 Show version history

I don't see anything out here for this currently. I am trying to trigger the onFieldChange function. Here is what the documentation shows:


This is not working with a checkbox. I am a total noob to the new javascript setup, so it's likely me. 

 

EDIT: looks like onFieldBlur works alternatively. Can someone confirm that checkboxes are currently incompatible with onFieldChange?

Thank you friends!

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on August 12, 2023

The key is to pass the field to the onFieldChange function. If you are using any of the find field function, they return an array so the individual field needs to be grabbed from that list. This code works for me
 

const checkbox = LFForm.findFieldsByFieldId(17)[0];
LFForm.onFieldChange((e) => {
  const checkboxValue = LFForm.getFieldValues(checkbox);
  console.log('checkbox change', checkboxValue)
}, checkbox);

It logs: checkbox change

  1. {value: Array(1), otherChoiceValue: ''}
    1. otherChoiceValue: ""
    2. value: ['Choice_1']
2 0
replied on August 14, 2023

Thanks this works perfectly for my scenario! 

Question: 
Since the checkbox is returned as an array, when we declare it with the index present ("[0]"), wouldn't that only work on change of the fist option in the checkbox? 

In my scenario, I only have one checkbox so it is fine but I could use some clarification regarding the index. Unless, the "[0]" always means the root of the field??

Thanks so much!!

0 0
replied on August 14, 2023

Its actually fields that are returned as an array. its a little different from jQuery, but if you notice the name of the function "findFieldsByFieldId" implies its returning a list. It is in fact the field and all checkbox values are accessible from getFieldValues

 

glad it works for you!

1 0
replied on August 14, 2023

thanks so much for the clarification!

0 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.