selected is the boolean property of <option> ; expressed in HTML attributes, its value is either empty (or not specified), or equal to the attribute name itself, indicating true (by convention).
$('#countryList option').filter(function () { return ($(this).text() == findText); }).attr('selected','selected');
This code sets the selected attribute for the first element of the match option. In fact, the code has several problems:
- The
.attr() function is called regardless of the filtering results. - The
selected attribute is not strictly an attribute, but a property.
It could be rewritten as follows:
$('#countryList option').each(function () { if (this.text == findText) { this.selected = true; return false;
source share