itbloger
홈
태그
방명록
programing tip
선택한 텍스트 검색
itbloger
2020. 6. 9. 08:22
반응형
선택한 텍스트 검색
에
element In the following: <select id="test"> <option value="1">Test One</option> <option value="2">Test Two</option> </select> How can I get the text of the selected option (i.e. "Test One" or "Test Two") using JavaScript document.getElementsById('test').selectedValue returns 1 or 2, what property returns the text of the selected option? function getSelectedText(elementId) { var elt = document.getElementById(elementId); if (elt.selectedIndex == -1) return null; return elt.options[elt.selectedIndex].text; } var text = getSelectedText('test'); If you use jQuery then you can write the following code: $("#selectId option:selected").html(); document.getElementById('test').options[document.getElementById('test').selectedIndex].text; selectElement.options[selectElement.selectedIndex].text; References: options collection, selectedIndex property: HTML DOM Select Object text property: HTML DOM Option Object exactly the answer of this question: Option text Property Under HTML5 you are be able to do this: document.getElementById('test').selectedOptions[0].text MDN's documentation at https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions indicates full cross-browser support (as of at least December 2017), including Chrome, Firefox, Edge and mobile browsers, but excluding Internet Explorer. The options property contains all the <options> - from there you can look at .text document.getElementById('test').options[0].text == 'Text One' You can use selectedIndex to retrieve the current selected option: el = document.getElementById('elemId') selectedText = el.options[el.selectedIndex].text this.options[this.selectedIndex].innerText If you found this thread and wanted to know how to get the selected option text via event here is sample code: alert(event.target.options[event.target.selectedIndex].text); Use the select list object, to identify its own selected options index. From there - grab the inner HTML of that index. And now you have the text string of that option. <select onchange="alert(this.options[this.selectedIndex].innerHTML);"> <option value="">Select Actions</option> <option value="1">Print PDF</option> <option value="2">Send Message</option> <option value="3">Request Review</option> <option value="4">Other Possible Actions</option> </select> Similar to @artur just without jQuery, with plain javascript: // Using @Sean-bright's "elt" variable var selection=elt.options[elt.selectedIndex].innerHTML; Easy, simple way: const select = document.getElementById('selectID'); const selectedOption = [...select.options].find(option => option.selected).text; 참고URL : https://stackoverflow.com/questions/610336/retrieving-the-text-of-the-selected-option-in-select-element
반응형
공유하기
게시글 관리
itbloger
저작자표시
'
programing tip
' 카테고리의 다른 글
이전 버전의 Android NDK는 어디서 찾을 수 있습니까?
(0)
2020.06.09
jQuery DataTables에서 특정 열에 대한 정렬 비활성화
(0)
2020.06.09
(1) 대.
(0)
2020.06.09
편집기에서 줄 바꿈 및 캐리지 리턴 확인
(0)
2020.06.09
RegExp.exec를 사용하여 문자열에서 모든 일치 항목을 추출하는 RegEx
(0)
2020.06.09
티스토리툴바