Reputation: 217
How can I set the default selected index of select tag using java script? My code working well, but I can't determine the default selected value (for example) the first value.
var CompanySelect = document.getElementById("ManagingCompaniesSelect");
if (result && result.length > 0) {
CompanySelect.options[CompanySelect.options.length] = new Option(_seletCompany, -1);
for (var i = 0; i <= result.length-1; i++)
{ CompanySelect.options[CompanySelect.options.length] = new Option(result[i].ManagingCompanyName, result[i].MangingCompanyGUId); }
}
var txtManagingCompany = '#ManagingCompaniesSelect' + ' option[value=-1]';
$(txtManagingCompany).attr("selected", "selected");
Upvotes: 0
Views: 88
Reputation: 12305
You can use the selectedIndex
property.
Example
CompanySelect.selectedIndex = 0;
// will select the first option by default and so on..
Upvotes: 1