IT tech Coding/javascript
html select문 value값을 자바스크립트로 가져오는 방법
Coding Life
2019. 7. 8. 15:26
자바스크립트로 웹을 구현할때, 자주 쓰는 기법인데, 생각이 날듯 날듯 안날때가 많아요~
한번 보면 바로 이해가 가는데, 쉽게 기억나지 않는 머리를 탓해야 할지... 쯧쯧...
select문은 웹프로그램에서 선택할때 자주 사용하는 것이다보니,
활용도가 높을 것 같습니다.
자바스크립트로 select문의 value값을 불러오기 어렵지 않아요~
물론 text값도 가져올 수 있답니다.
<html>
<body>
<select name="selectBox" id="selectBox">
<option value="가" selected>A</option>
<option value="나">B</option>
<option value="다">C</option>
</select>
</body>
<script type="text/javascript">
var target = document.getElementById("selectBox");
alert('선택된 옵션 text 값=' + target.options[target.selectedIndex].text); // 옵션 text 값
alert('선택된 옵션 value 값=' + target.options[target.selectedIndex].value); // 옵션 value 값
</script>
</html>
반응형