What's the difference between "selected" and "defaultSelected" when constructing the HTMLOptionElement?

From MDN :

defaultSelected - optional A
Boolean value that sets the selected attribute to a value, i.e. this will be the default on the element when the page is first loaded. If not specified, the default is false. Note that true does not set an option for the selected one if it is not already selected.

selected - optional A
Boolean value that sets the state of the selected parameter; the default is false (not selected). If omitted, even if defaultSelected is true, the option is not selected.

This is very incomprehensible to me. defaultSelected

determines "whether this value will be the default selected", but one sentence later it says "true does not set the option for the selected one if it is not already selected". So ... it doesn't set it to select? Does it only install it if it is already selected?

selected

also claims to set the selected state. If it is omitted, regardless defaultSelected

, the option is not selected. So ... defaultSelected

does nothing?

What's going on here?

+5


source to share


2 answers


The property defaultSelected

represents the initial (default) state of the element selection and corresponds to the selected HTML attribute. The property selected

represents the current state of the selection, and this is what the user actually sees in the browser.

When the user selects an option, only the property changes selected

, and the attribute and property defaultSelected

remain unchanged (although they can still be changed programmatically). You can use this, for example, to reset some of the default settings.

So I would say that you usually use either

 new Option(text, value, true, true)

      

or

new Option(text, value, false, false)

      



Setting different values โ€‹โ€‹for the 3rd and 4th parameters means that you are creating a parameter that is the default but is not currently selected, or vice versa.

I also find the docs quite confusing, but I think that

defaultSelected - & lt; ...> Note that true does not set the option for the selected if it is not already selected

just means that - if you set it defaultSelected

to true, it doesn't mean that the option will actually be selected, it just sets the option as the default. To select it, you must also set the property selected

.

In addition, when you add an attribute selected

to HTML, the corresponding DOM element gets properties defaultSelected

and is set selected

to true, so the initial and current values โ€‹โ€‹are the same.

Here is a helpful answer to a related question about DOM element attributes and properties: fooobar.com/questions/2126 / ...

+1


source


it seems to me that defaultSelected = true should be a solution on behalf of the user. I noticed that the "option" having a true setting for defaultSelected has an "selected" html attribute in the rendered html, but not a selected item in the list. hits me why, but I understand his intention somewhat.



0


source







All Articles