Make option defaultSelected
HTMLOptionElement.defaultSelected = true; // JS $('selector').prop({defaultSelected: true}); // jQuery
HTMLOptionElement MDN
If the SELECT element is already added to the document (statically or dynamically), HTMLFormElement.reset() - defaultSelected is used to set the Attribute- selected parameter and to survive it:
const EL_country = document.querySelector('#country'); EL_country.value = 'ID';
<form> <select name="country" id="country"> <option value="AF">Afghanistan</option> <option value="AL">Albania</option> <option value="HR">Croatia</option> <option value="ID">Indonesia</option> <option value="ZW">Zimbabwe</option> </select> </form>
The above will also work if you create the parameters dynamically and then ( only after that ) you want to set one parameter to defaultSelected .
const countries = { AF: 'Afghanistan', AL: 'Albania', HR: 'Croatia', ID: 'Indonesia', ZW: 'Zimbabwe', }; const EL_country = document.querySelector('#country');
<form> <select name="country" id="country"></select> </form>

Make parameter defaultSelected when dynamically creating parameters
To make the selected parameter when filling in the SELECT element , use the Option() MDN constructor
var optionElementReference = new Option(text, value, defaultSelected, selected);
const countries = { AF: 'Afghanistan', AL: 'Albania', HR: 'Croatia', ID: 'Indonesia',
<form> <select name="country" id="country"></select> </form>
In the demo above, Document.createDocumentFragment is used to prevent elements from rendering inside the DOM in a loop. Instead, a fragment (containing all parameters) is added to the selection only once.
SELECT.value vs OPTION.setAttribute vs OPTION.selected vs OPTION.defaultSelected
Although some (older) browsers interpret the OPTION selected attribute as a "string" state, the WHATWG html.spec.whatwg.org HTML specifications state that it should represent a logical selectedness
Selecting an option element is a logical state , initially false. Unless otherwise specified, when creating an element, its selection must be set to true if the element has the selected attribute.
html.spec.whatwg.org - Option selectedness
It can be correctly concluded that only the selected name in <option value="foo" selected> enough to set the true state.
Comparative test of various methods
const EL_select = document.querySelector('#country'); const TPL_options = ' <option value="AF">Afghanistan</option> <option value="AL">Albania</option> <option value="HR">Croatia</option> <option value="ID">Indonesia</option> <option value="ZW">Zimbabwe</option> ';
<form> <select name="country" id="country"></select> </form>
Although test 2. using .setAttribute() at first glance seems to be the best solution, since both the property of the element and the attribute are unison, it can lead to confusion , especially because .setAttribute expects two parameters:
EL_select.options[1].setAttribute('selected', false); // <option value="AL" selected="false"> // But still selected!
actually make the choice selected
Should I use .removeAttribute() or perhaps .setAttribute('selected', ???) for a different value? Or should you read the state using .getAttribute('selected') or .hasAttribute('selected') ?
Instead, test 3. (and 4.) using defaultSelected gives the expected results :
- The
selected attribute as the named selection state . - The
selected property in an element object with a boolean value .