add() 메소드 선택
예시
드롭다운 목록 끝에 "Kiwi" 옵션을 추가합니다.
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
정의 및 사용
add() 메서드는 드롭다운 목록에 옵션을 추가하는 데 사용됩니다.
팁: 드롭다운 목록에서 옵션을 제거하려면 remove() 메서드를 사용하십시오.
브라우저 지원
Method | |||||
---|---|---|---|---|---|
add() | Yes | Yes | Yes | Yes | Yes |
통사론
selectObject.add(option, index)
매개변수 값
Parameter | Description |
---|---|
option | Required. Specifies the option to add. Must be an option or optgroup element |
index | Optional. An integer that specifies the index position for where the new option element should be inserted. Index starts at 0. If no index is specified, the new option will be inserted at the end of the list |
기술적 세부 사항
반환 값: | 반환 값 없음 |
---|
더 많은 예
예시
드롭다운 목록 시작 부분에 "Kiwi" 옵션을 추가합니다.
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
예시
드롭다운 목록의 인덱스 위치 "2"에 "Kiwi" 옵션을 추가합니다.
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
예시
드롭다운 목록에서 선택한 옵션 앞에 옵션을 추가합니다.
var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
var option = document.createElement("option");
option.text = "Kiwi";
var sel = x.options[x.selectedIndex];
x.add(option, sel);
}
❮ 개체 선택