Select a radio button automatically

Hi, after adding the values in two fields, I would like to use that value to select the default value of a radio button field. For example, if the field values are 2 and 3, I would like to have the 5 value preselected on radio button field that goes from 1 to 10. If the values were 3 and 6 then the value 9 would be pre-selected.

Any help most appreciated.

I’m not sure Gravity Forms can auto select another fields choices based on another fields selection.

Are you ultimately just needing to get the total value of the selections?

Hi, creating the total is fine. I just use a calculation in a number field. The challenge is to use that value to preselect the radio button. It’s possible using code to change the isSelected value, but I’m not sure exactly how.

I’ve found and tested this code, which sets the default radio button using the isSelected attribute. I am able to put in a static value (in this case adding the value 7.0 into radio button field 150) but I don’t know how to get the value of a number field into a variable that would replace the static value.

add_filter( 'gform_pre_render_1', 'my_populate_radio' );

function my_populate_radio( $form ) {

  foreach( $form['fields'] as &$field ) {
    
    if( 150 === $field->id ) {
     
      foreach( $field->choices as &$choice ) {  
	  
        if( '7.0' === $choice['value'] ) {
          $choice['isSelected'] = true;
        }
      } 
    } 
  } 
  return $form;
}

If the snippet is working for you then you could always use Gravity Wiz live merge tag plugin. You would take the radio buttons individual selections, copy them individually into multiple number fields, copy the number fields merge tags into the calculation field, then copy the value of the calculation into the radio button you have pre-selected.

In which case if this works for you, you really just need to add one radio button, and pre-select it in the radio button field. I don’t think you need the snippet if you just need to pre-select an item.

Hope this helps!

I have Populate Anything so I can use that, but I’m not sure I follow your idea correctly. Could you explain it with an example?

Sure thing! Populate Anything only works if the data has already been submitted. So that particular plugin would not work in the case. Whereas the Live Merge Tag plugin can take live results that the visitor is entering and essentially duplicate the values in other fields automatically without needing to submit the data before hand. This is literally in real time and doesn’t depend on using your pre-existing submissions.

For example:

Field 1.
What’s your favorite number?
The answer the visitor enters is 5.

Field2.
What’s your lucky number?
The answer the visitor enters is 3.

So since we need to perform a calculation, we need to send that data ‘5’ and ‘3’ into two number fields. With live merge tags the merge tag would be something like field1:question1, or field2:question1.

You would take each merge tag and add them individually into a hidden number field. Now since this is a number, it can be used in a calculation. So you would cascade the live merge tags all the way until you get the correct value into the radio button you have pre-selected. IE. Take the calculation field live merge tag and it to the radio button.

Tadah!

Let’s see if I’ve got it: Let’s say there are four radio button selections (1,2,3,4) and I want 3 selected. Are you saying I should pre-select the first one and then use the merge tags to change the values in each selection, making sure that the number I want be be selected is in the first position? Let’s say I want the number 3 to be selected, would it be like this?

3 (selected), 1, 2, 4

If that’s the case then I don’t think that will work for me, because I need the numbers to go up linearly 1, 2, 3 (selected), 4

So the one radio button is the total. So you would not need four radio buttons at all. You would need one radio button which you go ahead and pre-check it.

All you’re doing is automatically populating the value of the radio button which reflects the correct total value of the two fields you’ve collected data from.

The value is dynamic so it will change based on the users input automatically for you.

Sorry, but I need the user to be able to possiby select the other values too. The calculation is to find an estimated time (3 hours) but the user might want to select 1, 2 or 4 hours.

Ok, if I’m understanding correctly.

You would need the dynamic field, which tells them the estimated time.
Then you can also add another field which let’s the user select 1, 2, or 4 hours.

So essentially you would have one dynamic field (used for estimated time)
and one static field which the user can manually check a time.

Just so you know, the link I sent above says Populate Anything, but the article link goes to the live merge tag section. Wanted to clear that up so that we’re on the same page. Just in case.

It’s not all that elegant though, because I’d have

Estimated time: 3 hrs
Other selections: 1, 2, 3, 4 hours

In fact, that’s what I’ve got now.

I’d really just like to preselect the 3 in the second row

I think I may not be understanding you perfectly. If you just want to pre-select 3 then why don’t you just pre-select 3? Is this all you needed to do? Lol.

Hope this helps.

The hours calculaion could come up with the number 1,2,3, or 4. Let’s say it comes up with 3.

I want the user to be able see that the calculation value (3) is preselected on the list, but also let them choose the other possibilities. So they’d intially see:

1, 2, 3 (selected), 4

But they thought the job would only take two hours then they could change it to:
1, 2 (selected), 3, 4

it has to be a radio button, because they can only choose one selection. Sorry, if this is con
fusing!

Gotcha! Gotcha! Gotcha!

So yes add your radio button. There will be four options or more. 1, (dynamic value populated with calculation), 3, 4, 5, 6, 7, 8, 9, 10 (however many you need)

Toggle the circle button next to the dynamic field, just like in the screenshot I sent in my previous post.

Then you would need use the live merge tags plugin to send the two options into number fields. Then the live merge tag for the numbers field into the calculation field. Lastly, you need to add the live merge tag from the calculation field into the second choice of your radio button.

This would allow the two values to be calculated and used as the pre-selected dynamic estimated time option, also allowing them to change this option to another choice.

Yes, I get that, but then the options won’t be in order: They’ll be 3 (selected), 1, 2, 4 and I’m not sure how I’d even figure out how it would know that 3 was already in use.

I think the easiest way would be to modify the code snippet to add the calculation result as a variable in place of the number 7.0 that’s in there just now.

Let me see if I can get @chrishajer from Gravity Forms in here. He’s :ok_hand:

1 Like

Has anyone had any further thoughts on this? I really just need to know how to add the content of a number field into a variable.

Hi Michael. Using gform_pre_render like that will not work, because the pre-render code is run before the form is shown. It sounds like you need to let the user make a couple choices, and then add those two choices, and pre-select that sum as the selected option in another field. Is that correct?

Hi Chris. Yes that’s correct.