Sherly Kim Sherly Kim - 8 months ago 25
Ajax Question

ajax get text of the option value in html

I have a code that lists all the values i want written :

<option th:each = "iName : ${iNames}" th:value = "${iName}" th:text = "${iName}" th:selected="${selectedIName == iName}"

and I have setSelectedName in my .js file

function setSelectedName() {
$('#NameForTypeSearch').ready(function() {
type: 'POST',
data: {keyname:$('#NameForSearch option:selected').text()}

I have text box named
and everytime the user selects an option, I want to use ajax to automatically input the selected text in
. I can't get to work on my html..


Try using the jquery val function to get the value of the option element, as well as to set the value of the input element.

<script src=""></script>
<select id="sel">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<input type="text" id="output"/>