How to select multiple items from listbox in jquery using comma separated value?

Posted by Sheonarayan under jQuery category on | Points: 40 | Views : 4943
In general, when we are getting comma separated value from database, we need to split and loop to select multiple items from the ListBox.

var employeeIds = '@ViewBag.EmployeeIds'; // comma separated value
var array = employeeIds.split(',');
for (var i = 0; i < array.length; i++) {
if (array[i].length > 0) {
$('#EmployeeIds option[value=' + array[i] + ']').attr("selected", true);

Here we are assuming that ViewBag.EmployeeIds have comma separated value, #EmployeeIds is the id of the listbox.

Comments or Responses

Login to post response