I want to append option in multiple select, option value come from ajax responce I using jquery-select2 for multiple select feild My script code
$.ajax({
type: "POST",
data: {'tc_agency_code': agencycode,'tc_agency_name': agencyname,'tc_agency_address':address,'tc_contact_no':contactno,'tc_port_id':port,'tc_port_id':port,'tc_agency_description':description,'xhttp_call':'Y'},
url: "<?php echo $this->baseurl(); ?>/agency/add",
success:function(response){
alert(response);
if(response =='null') {
error ='Agency name are already exist.';
$( "#agency_name_error" ).html( error );
return false;
}
var result = $.parseJSON(response);
alert(result.tc_agency_id); //this alert showing id like '190'
alert(result.tc_agency_name); // this alert showing text like 'test'
$('#tc_agency_ids').append($('<option></option>').attr('value', "'"+ result.tc_agency_id +"'").text("'"+ result.tc_agency_name +"'"));
/* $('#tc_agency_ids').append($('<option>', { // i searched that this commented code is also correct for append the value in multiple select but not working in my code
value: result.tc_agency_id,
text : result.tc_agency_name
})); */
$('#tc_agency_ids option[value='+ result.tc_agency_id +']').attr('selected', true);
$.fancybox.close();
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});