Nagesswara T Nagesswara T - 4 months ago 11
HTML Question

Not able add multiple option to select

function successCallback(caRecords) {

var x = document.getElementById("custAccount"); // select
var option1 = document.createElement("option"); //options
//var accno = 0;

// caRecords i am fetch from MS CRM
var count = caRecords[0].results.length;

if (caRecords != null && count > 0) {
alert("records are not null");

for (var i = 0 ; i < count; i++)
{
var text = caRecords[0].results[i].new_name;
// alert(text + "J=" + j);
option1.text = text;
option1.value = j;
x.add(option1);
j++;

}
}


I got six records and try to insert that values into select as option. It showing last value of my 6 values.

enter image description here

Can anyone help me to improve my code?

Answer

You can iterate your values like this...

function successCallback(caRecords) {
         var x = document.getElementById("custAccount"); // select
         var options = "";
         var count = caRecords[0].results.length;
         if (caRecords != null && count > 0) {
           alert("records are not null");
           for (var i = 0; i < count; i++) {
             options += "<option value=" + j + ">" + caRecords[0].results[i].new_name + "</option>";
             j++;
           }
           x.innerHTML = options;
         }