Option value selected=selected in a form | Community
Skip to main content
New Participant
December 3, 2020
Question

Option value selected=selected in a form

  • December 3, 2020
  • 1 reply
  • 2728 views

Hello everyone,

 

I have a form with a Country select field. The first option is at follows:

<option value="">Select...</option>

 

What I need in reality is:

<option selected="selected" value="">Select...</option>

 

I've clicked on the Default checkbox within the Editor, without success.

 

I think this can be done via some simple JS script, but is there a way to avoid that and do it from the Editor?

 

Thanks,

Yavor

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

SanfordWhiteman
New Participant
December 3, 2020

I can tell you how to do it, but first I need to question the UX.

 

A selected empty value will not make any changes on the Marketo server (it will not empty the field). So it's no different from having the field be optional. Why would you want to have a meaningless entry be the default selection?

New Participant
December 3, 2020

Hi Sanford,

 

thanks for the reply.

 

I know this is now the best thing to do from UX perspective. It looks like we have a script that will work only if we have selected="selected" on the Select option.

 

 

SanfordWhiteman
New Participant
December 3, 2020

Very strange... I worry at what other assumptions that script makes. Most 3rd-party attribution libraries (if that's what this is) don't work correctly with Marketo forms, because they aren't built to understand the Marketo forms events.

 

Anyway:

MktoForms2.whenReady(function(mktoForm){ let formEl = mktoForm.getFormElem()[0], countryPlaceholderOption = formEl.querySelector("select[name='Country'] option[value='']"); countryPlaceholderOption.selected = true; countryPlaceholderOption.setAttribute("selected","selected"); });