본문 바로가기
자바스크립트

자바스크립트로 드롭다운 박스에 배열 값을 넣어 보자

by 개발하는 늑대 2020. 8. 17.
728x90

요세 들어 순수하게 Jquery 만 쓰기 보다는 자바 스크립트를 섞어 쓰는 경향이 나타 나는 것 같다.

그래서 구글링을 하다 보니 아래 와 같은 소스를 찾았다. 

원문 링크를 걸어 두니 참조 해 보시기 바란다. 

아래 에 소스도 적어뒀다.

 

출처: https://stackoverflow.com/questions/17001961/how-to-add-drop-down-list-select-programmatically

 

How to add Drop-Down list (<select>) programmatically?

 

 

var myParent = document.body; 

//Create array of options to be added 
var array = ["Volvo","Saab","Mercades","Audi"];

//Create and append select list 
var selectList = document.createElement("select"); 

selectList.id = "mySelect"; 
myParent.appendChild(selectList); 

//Create and append the options 
for (var i = 0; i < array.length; i++) { 
	var option = document.createElement("option");
	option.value = array[i];
	option.text = array[i];
	selectList.appendChild(option); 
}
728x90