Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have company drop down list.i want to make company name as default.i retrieve that value from database through passing id of company name.

I have used this code,but it's not working

What I have tried:

var sel = document.getElementById('Company_ID');
sel.selectedIndex = 1;
Posted

1 solution

In disbelieve, I tested you code. Exactly as I expected, it worked.

Conclusion: the bug is elsewhere, not in the code you show. For example, the select element you want to use might not have the attribute id="Company_ID". Another possibility is: some other JavaScript code is wrong or even lexically incorrect, so your fragment of code is never executed. I case of lexical bug or exception in code, the script will fail silently, unless you take special measures to reveal the problem.

Advice: use JavaScript debugger. You will locate the problem in no time.

—SA
 
Share this answer
 
Comments
Hussain Javed 12-Feb-16 2:15am    
here is my view code
@model Model.Student_Registration
@using CloudPayroll.Helpers
@using Model.Models

@{
ViewBag.Title = "Create";
}

<script type="text/javascript">





var sel = document.getElementById('Company_ID');
sel.selectedIndex = 1;



$(function () {





$("#formID").validationEngine();

$("input:checkbox").live("click", function () {
if ($(this).is(":checked")) {
$(this).attr("value", "True");
} else {
$(this).attr("value", "False");
}
});
$("#btnCancel").live("click", function () {
$("#formID").validationEngine("hide");
});
$("#DATE_OF_BIRTH").datepicker({
changeDate: true,
changeMonth: true,
changeYear: true,
maxdate: new Date()
});
$("#DATE_OF_JOINING").datepicker({
changeDate: true,
changeMonth: true,
changeYear: true,
maxdate:new Date()
});
});

$(document).ready(function () {




$("#PHOTO").live("change", function (data) {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview')
.attr('src', e.target.result)
.width(80)
.height(110);
};
reader.readAsDataURL(this.files[0]);
}
});

$('#PHOTO').live("change", function () {
var ext = $('#PHOTO').val().split('.').pop().toLowerCase();
if ($.inArray(ext, ['gif', 'png', 'jpg', 'jpeg']) == -1) {
alert('Invalid Image Format!');
}
});



//change event for the COUNTRY_ID combo
$("#COUNTRY_ID").live("change", function () {
var CountryID = $(this).attr("value");

var ListSt = $('#STATE_ID');
var ListCt = $('#CITY_ID');

ListSt.empty();
ListCt.empty();
ListSt.append('<option value="0">Select</option>');
ListCt.append('<option value="0">Select</option>');
//call get state method experience master controller to get state list according to the country selected
$.getJSON("/../EXPERIENCE_MASTER/GetStates?CountryID=" + CountryID, null, function (data) {
$.each(data, function (index, StateCC) {
ListSt.append($('<option/>', {
value: StateCC.STATE_ID,
text: StateCC.STATE_NAME
}));
});
});

});
//change event for the state combo
$("#STATE_ID").live("change", function () {
var StateId = $(this).attr("value");
var ListCt = $('#CITY_ID');
//same for storeing city in ListCT

ListCt.empty();
//here we emplt the list of city
ListCt.append('<option value="0">Select</option>');
//now append the value 0 and text select to the list of listct
$.getJSON("/../EXPERIENCE_MASTER/GetCities?StateId=" + StateId, null, function (data) {

$.each(data, function (index, CityCC) {
ListCt.append($('<option/>', {
value: CityCC.CITY_ID,
text: CityCC.CITY_NAME
}));
});
});


});
});

</script>

@*<div>
<table cellpadding="0" cellspacing="0" class="dyntable" width="100%" id="example">
<tr>
<td width="20%">
SCHOOL NAME
</td>
<td width="20%">@Html.DropDownList("Company_ID", (IEnumerable<SelectListItem>)ViewBag.Company_ID, "All", new { style = "width: 250px;", id = "Company_ID" })
</td>
</tr>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900