//선택자를 통해 원하는 select 를 가져온다
let select = document.getElementById('select');
//select.selectedIndex --> selected된 옵션 번호
select = select.options[select.selectedIndex].value;
select.selectedIndex 값 이용
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex
HTMLSelectElement.selectedIndex - Web APIs | MDN
The HTMLSelectElement.selectedIndex is a long that reflects the index of the first or last selected <option> element, depending on the value of multiple. The value -1 indicates that no element is selected.
developer.mozilla.org
또는
//선택자를 통해 원하는 select 를 가져온다
let select = document.getElementById('select');
//select.selectedOptions --> selected된 옵션 리스트
//select 가 multiple 일 경우 선택된 여러개의 옵션 값을 가져올 수 있음
// value 또는 label 을 통해 가져올 수 있다
let selectedVal = select.selectedOptions[0].value;
let selectedTxt = select.selectedOptions[0].label;
selectedOptions = select.selectedOptions;
for(let i=0; i<selectedOptions.length; i++){
console.log(selectedOptions[i].value);
console.log(selectedOptions[i].label);
}
select.selectedOptions 값 이용
https://developer.mozilla.org/ko/docs/Web/API/HTMLSelectElement/selectedOptions
HTMLSelectElement.selectedOptions - Web API | MDN
The read-only HTMLSelectElement property selectedOptions contains a list of the <option> elements contained within the <select> element that are currently selected. The list of selected options is an HTMLCollection object with one entry per currently selec
developer.mozilla.org
'javascript > javascript' 카테고리의 다른 글
여러개를 제어할때 쓰는 querySelectorAll (236) | 2022.08.31 |
---|---|
select 플러그인 (421) | 2022.08.30 |
javascript 여러 엘리먼트에 한번에 이벤트리스너 등록하기 (418) | 2022.08.29 |
swiper 플러그인 옵션정리 (426) | 2022.08.22 |
swiper - 이미지 슬라이드 플러그인 (0) | 2022.08.08 |
댓글