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

Question

Question

Proper way to do an if statement on a radio button (Javascript)

asked on December 3

I've got a text field that I want populated based on the response to a radio button question.  The code I have is 

fieldID 140 is the radio button.  When select "No" on the form, the if statement isn't kicking in.  I'm not sure what I'm doing wrong as I've gotten this code from Answers.  (I'm using the Modern Form designer.)

0 0

Replies

replied on December 3

Line 4 you're not setting a field value, it should be 

LFForm.setFieldValues({ fieldId: 10 }, fieldValue)

 

2 0
replied on December 3

I made that change, but it didn't work.  I don't think it's looking at the if statement properly because I'm not seeing the console.log("Test111").

This is what I see in Developer Tools:

0 0
replied on December 3 Show version history

I don't think the .value should be there on line 2.

LFForm.getFieldValues() returns the values, not the element.

  • If it is single-valued you get the value
  • If it is multiple (i.e., multiple fields, a table, etc.) you get an array
0 0
replied on December 4

I'd add a console above line 3 with the variable fieldValue. Without seeing your form I don't know the context of fields you're working with but the console statement will show you whats wrong. If you remove the .value from line 2 it will log the entire field value returned so you know how to pull exactly what you're looking for.

0 0
replied on December 4

Here are screenshots from a test form with just a single line field and Radio Button and a hardcoded value to insert into the single line field.

And the results from first hitting Yes and then switching to No

1 0
replied on December 4

I copied your code, but kept my same fieldId's:

As a result, now I'm not seeing any of my console logs:

0 0
replied on December 4

Ah well that makes it easy! you capitalized the D in fieldId on line 2 of the code

1 0
replied on December 4 Show version history

You still added .value which is going to return undefined

You don't need that part because the API method returns the values, not the element.

 

0 0
replied on December 4

Also, line 4 is missing the ; at the end.

0 0
replied on December 4

@████████ .value is still needed because radio/checkboxes return an object instead of a string. You can see the value returned in Bert's second screenshot

 

Also ; isn't needed in JS, I definitely recommend consistency and using it but it won't throw an error or stop code execution without it

1 0
replied on December 4 Show version history

@████████ I do not call .value on line 2.  Instead I get the object and then call .value on the object created by line 2 in line 4.

 

While the code may be shortened, then you need to adjust to use the correct variable.  In their version of my code, they are trying to call .value in line 4 on an object that does not have a .value method.

0 0
replied on December 4 Show version history

@████████ Good to know; that does make sense. Is that mentioned somewhere in the documentation?

0 0
replied on December 4

Your code is correct, just slightly different than the original post.

And yes the return types are crudely documented on the setFieldValues not the getField values
https://doc.laserfiche.com/laserfiche/en-us/Content/Resources/BusinessProcesses/Javascript-and-CSS/TheLFFormObject.htm?Highlight=lfform#setFieldValues (cloud docs applicable to self-hosted v12+)

I gave myself an empower deadline to fix this documentation page so stay tuned haha

1 0
replied on December 5

Thanks peeps!  I got it to work with this code:

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

Sign in to reply to this post.