SharePoint populate Choice field value using jQuery
-
22-01-2021 - |
Question
var ArrivalCity = (data.aircraftView[0].arcftSchedList[0].tripArrivalCity);
$('select[title="Select the Destination"]').val(ArrivalCity);
var ArrivalCity1 = (data.aircraftView[1].arcftSchedList[0].tripArrivalCity);
$('select[title="Select the Destination"]').val(ArrivalCity1);
I am trying to populate the choice field from jquery on the SharePoint list. I am able to get the values but it is not getting stored in my choice field defined. I have multiple values coming into the arrival city and departure city, so I am getting those values from rest api. I am not sure why they are not getting populated in my choice field. I have attached a screenshot of the selector. Can anyone please help me with this?
Solution
Append options to select element.
$('select[title="Select the Destination"]').append($('<option></option>').val("value1").html("text1"));
$('select[title="Select the Destination"]').append($('<option></option>').val("value2").html("text2"));
Update:
var Options = [{ "Value": "Value1", "Text": "Text1" },
{ "Value": "Value2", "Text": "Text2" },
{"Value": "Value3","Text":"Text3"}]
$(function () {
$.each(Options, function () {
$('select[title="Select the Destination"]').append($('<option></option>').val(this.Value).html(this.Text));
})
//$('select[title="Select the Destination"]').append($('<option></option>').val("value1").html("text1"));
//$('select[title="Select the Destination"]').append($('<option></option>').val("value2").html("text2"));
})
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange