Option value selected=selected in a form

Yavor_Todorov
Level 3

Option value selected=selected in a form

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.

default.png

 

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

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Option value selected=selected in a form

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?

Yavor_Todorov
Level 3

Re: Option value selected=selected in a form

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
Level 10 - Community Moderator

Re: Option value selected=selected in a form

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");
});