0

(In Javascript) I am doing a GET request inside typefunc() and getting result (variable res) in this format:-

[{"Name":"Button1","ID":"A1"}, {"Name":"Button2","ID":"A2"}, {"Name":"Button3","ID":"A3"}]

How do I populate the below mentioned drop-down using this data?

<select id="type" class="form-control" onchange="typefunc();" required>
    <option selected>Choose...</option>
</select>
0

1 Answer 1

0

This is a demo showing how to create option elements to append to the target dropdown based on the options object:

const options = [
  {"Name":"Button1","ID":"A1"},
  {"Name":"Button2","ID":"A2"},
  {"Name":"Button3","ID":"A3"}
];

populateDropdown();

function populateDropdown(){
  const dd = document.getElementById('type');
  for(let option of options){  
    const newOption = document.createElement('option');
    const optionText = document.createTextNode(option.Name);
    newOption.appendChild(optionText);
    newOption.setAttribute('value',option.ID);  
    dd.appendChild(newOption);
  }
}
<select id="type" class="form-control" onchange="typefunc();" required>
    <option selected>Choose...</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.