Developer
Developer

Reputation: 444

appending new option to dropdown list dynamically as selected

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

Answers (3)

A J
A J

Reputation: 2140

Try this

$('#department').append(new Option(response.departmentname, response.departmentid))
$('#department').val(response.departmentid);

check out this fiddle

on click of a button

Upvotes: 2

dfsq
dfsq

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).

Demo: http://jsfiddle.net/dfsq/S5fp7/

Upvotes: 5

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

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

Related Questions