Home:ALL Converter>Set the selected index of select tag using JavaScript

Set the selected index of select tag using JavaScript

Ask Time:2013-07-29T17:18:49         Author:Alaa

Json Formatter

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");

Author:Alaa,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17920183/set-the-selected-index-of-select-tag-using-javascript
yy