FrontEnd/JavaScript & jQuery
JavaScript :: select option text, value 출력
초록 (green)
2016. 3. 11. 19:40
option 사이즈가 4개인 배열이 있다.
이 배열의 value 값과 text 값을 한꺼번에 출력해보도록 하자.
배열이기 때문에 반복문을 사용하여 출력할 수 있다.
함수를 선언하고 getElementById 메소드를 이용하여 select에 접근한다.
옵션들의 text 값은 options[i].text (mySelect라는 select의 옵션들의 text 값)
옵션들의 value 값은 options[i].value (mySelect라는 select의 옵션들의 value 값)
를 사용하면 어떠한 옵션 항목을 선택하던 전체 옵션 배열 값들이 출력된다.
이렇게 한꺼번에 출력되는 것을 볼 수 있다.
만약 옵션 배열 중 선택한 옵션인 특정 text 값이나 value 값을 출력하고 싶다면
options[id값.selectedIndex].value
options[id값.selectedIndex].text
를 사용하면 된다.