Skip Navigation

[Resolved] How to prefill default value in dropdown box?

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

Problem:
How to set default value for view's filter dropdown select or how to prefill the default value for view's filter dropdown select

Solution:
You can use custom jQuery and add it to your view's filter Js box.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/how-to-prefill-default-value-in-dropdown-box/#post-591999

Relevant Documentation:

This support ticket is created 7 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 3 voices.

Last updated by bartV-2 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#591549
prefilled-field.png

Hi, I have a dropdown box and I don´t know how to set the first value as a default. In admin menu for created CPT the first one is automatically preselected.

#591555

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please tell me how did you configure that field? using post field? If yes: could you please share screenshot?

As I understand you are displaying the field in admin - correct?

#591572

Hi, this field is visible and it´s in custom search:

[wpv-control-postmeta field="wpcf-sezona" type="select" order="none" url_param="wpv-wpcf-sezona"]

I just mentioned, that in backend in CPT editor it displays correctly.

#591573

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you can add attribute default_label . For example:

[wpv-control-postmeta field="wpcf-sezona" default_label="Please select one" type="select" order="none" url_param="wpv-wpcf-sezona"]

Could you please try with above sample code and let me know if that works for you.

#591576

I know that there is an attribute default_label= but I need to have a preffiled value instead of it.

#591577

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - this is what I get when I set default_label attribute. Please check following screenshot.
=> hidden link

it look like I must not understand your requirement well. Could you please share problem URL and tell me when you would like to prefill? Do you mean you would like to add more options to dropdown select? Few more screenshots with more explanation about your issue will help me to understand your issue and that will help me to guide you in right direction.

#591773
dropdown.png

Please, see the attachment. The 3 fields are values. I need to have the first value preselected in my Custom Search.

#591999

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I understand.

You can easily do it by adding simple jQuery code to your view's filter section's JS box:

[php]
jQuery(document).ready(function($) {

jQuery("select[name^='YOUR-SELECT-NAME'] option[value='OPTION-VALUE']").attr("selected","selected");

});
Where
Replace "YOUR-SELECT-NAME'" and first option "OPTION-VALUE" with your original values.

If you do not know how to do it, just share problem URL where I can see the dropdown select box and I will give you exact code.

#592292

Thank you, that´s what I was looking for.

#629580
types.png

This is my filter code :

[wpv-filter-start hide="false"]
<div class="form-group wpcf-filter">
[wpv-control-postmeta field="wpcf-jaar" type="select" url_param="wpv-wpcf-jaar"]
[wpv-control-post-taxonomy taxonomy="publicatie-type" type="select" url_param="wpv-publicatie-type"]
</div>
[wpv-filter-end]

And jquery :
jQuery(document).ready(function($) {
jQuery("select[name^='wpv-wpcf-jaar'] option[value='" + (new Date()).getFullYear() + "']").attr("selected","selected");
});

But the data shown is not filtered on the default year ...