Reputation: 444
Appending new Option using JQuery dynamically it works fine
$('#department').append(new Option(response.departmentname,response.departmentid));
I want to make it as a selected ?
Upvotes: 2
Views: 240
Reputation: 2140
Try this
$('#department').append(new Option(response.departmentname, response.departmentid))
$('#department').val(response.departmentid);
Upvotes: 2
Reputation: 193301
You can set the 4th options to true:
new Option(response.departmentname, response.departmentid, false, true)
From documentation:
"selected" (a boolean to indicate whether the option will be "selected"; if omitted, it will not be selected).
Upvotes: 5
Reputation: 67217
Try to set its(select element with id
department) value via chaining
,
$('#department')
.append(new Option(response.departmentname,response.departmentid))
.val(response.departmentid);
Upvotes: 2