$ select.val ("SomeValue")
This is good, in the general case, when it selects not multiple , and you do not have two different <option> with the same value . If in this case I will go with this as the most widely read option.
$ select [0] .selectedIndex = 1
This is more straightforward, so slightly faster, and you need to be unique for the case when you have several options with the same value.
If you can have multiple -select, you should get / set the selection of each option separately:
$select[0].options[1].selected= true;
But:
$ option.attr ("selected", "selected")
Not the best approach. The problem with attr() is a nasty hack for accessing the DOM properties and HTML attributes, as if they were the same, trying to hide the difference with you. In this case, attr() should set the selected DOM property, which is a boolean. So attr('selected', true) will make more sense; 'selected' as a string value also works, but only because all non-empty string values ββare "true in JavaScript".
If you actually set the HTML selected attribute here, this would not affect the selected parameter, because the selected attribute actually matches the defaultSelected property, not the selected ! The selected property reflects the contents of the execution form modified by the user; defaultSelected reflects the actual attribute in the document containing the initial selection state.
(Except IE, due to an error in the implementation of the default values, as well as in other browsers in some situations that are too confusing and confusing. To assume: do not try to install selected HTML from the script, as the results may be unpredictable. DOM properties. The same applies to value / defaultValue for inputs and checked / defaultChecked for checkbox / radio.)
bobince
source share