Skip Navigation

[Resolved] How to set a default value in a form

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to set a default value for the dropdown (HTML Select) in the form?

Solution:

Use jQuery code to select the dropdown and choose the option in question. A sample code:

$('#wpv_control_select_wpcf-nights').val('2');

You can add it in the JS editor section of the Toolset content template or view that you add the form.

Documentation:

https://toolset.com/course-lesson/adding-custom-javascript-to-views-templates-and-archives/

This support ticket is created 2 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by nickT-2 2 years, 1 month ago.

Assisted by: Christopher Amirian.

Author
Posts
#2464879
Screenshot 03 2022-09-26 102349.png
Screenshot 02 2022-09-26 101946.png
Screenshot 01 2022-09-26 101928.png

We are using a View to insert a 'first step' Check Availability form, which then continues on to a programmed url (with the variables in the url).

But we would like to change the 'nights' drop down to default to 2 nights ... currently set to 1.

I've changed the default in the field's settings (screenshot 1) - but it's not changed anything (screenshot 2).

Please can I check if this is possible to do from the 'wpv-control-postmeta' shortcode instead? I've tried adding default="2" but it didn't seem to work.

Here is the code so far (screenshot 3) and below:

      <span>Nights</span> [wpv-control-postmeta field="wpcf-nights" type="select" url_param="nights"]

Thanks
Nick

#2465079

Christopher Amirian
Supporter

Languages: English (English )

Hi Nick,

The [wpv-control-postmeta] shortcode does not have a defulat value.

Please use the shortcode below to add a default value for the Form field:

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field

For more information:

https://toolset.com/forums/topic/pass-atribbute-arguments-to-a-cred-edit-form/

Thanks.

#2465085

Hi,
I'm not actually using a Cred form. I'm using the filter setting on a view as a workaround to grab the selection and then forward onto the actual booking site which picks up the variables via the url.

In case it helps and you spot a solution, here is my complete code for the filter:

[wpv-filter-start hide="false"]
[wpv-filter-controls]
  <div class="wpb_column column_container col-sm-4" align="center" style="margin-left:-10px">
      <span class="check-availability-label">Arrive</span><br> [wpv-control-postmeta field="wpcf-arrival" type="date" url_param="arrive-date-picker"]
	</div>
	<div class="wpb_column column_container col-sm-4" align="center" style="margin-left:-15px">
      <span class="check-availability-label">Nights</span> [wpv-control-postmeta field="wpcf-nights" type="select" url_param="nights"]
  </div>
  <div class="wpb_column column_container col-sm-4">
    <div style="padding-top:15px;">
  [wpv-filter-submit name="Check Availability" type="button" output="bootstrap"]
    </div>
<input type="hidden" name="arrive" value="">
<input type="hidden" name="Chain" value="8565">
<input type="hidden" name="template" value="Responsive_GIHL">
<input type="hidden" name="shell" value="RBE_GIHL2">
<input type="hidden" name="hotel" value="22921">
<input type="hidden" name="adult" value="2">    
  
  [/wpv-filter-controls]
  [wpv-filter-end]

Is there a way to set the default value, other than removing it from the Nights dropdown as an option

#2466459

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Unfortunately, the "wpv-control-postmeta" shortcode does not have any default value selection functionality. It might be possible with Javascript.

Would you please provide the link tot the page that contains the "Nights" dropdown and tell me which option should be selected?

It might be possible.

Also, you can provide login information by setting the next reply as private if there is a need for login to access that page.

#2466633

Christopher Amirian
Supporter

Languages: English (English )

Hi Nick,

I went to the View that contained the dropdown and under the JS Editor added the code below:

$('#wpv_control_select_wpcf-nights').val('2');

It selects the SELECT html tag with the ID of "wpv_control_select_wpcf-nights" and selects the second option.

Please consider that it will take a few seconds for the Javascript code to render and it is a normal behavior.

Thanks.

#2466899

Fantastic .. works great.
Thank you so much for having a look and suggesting JS code!!