0

I am trying to send a value to an based on a selection from a dropdown list such as . I want to fetch the value of possiblePhone.id and send it to .

<script>
function copyTextValue() {
    var text1 = document.getElementById("source").value;
    document.getElementById("destination").value = text1;

}
</script>


<div>
<select th:field="${possiblePhones}">
    <option value="0">select phone</option>
    <option id="source" onselect="copyTextValue()"
            th:each="possiblePhone : ${possiblePhones}"
            th:value="${possiblePhone.id}"
            th:text="${possiblePhone.model}"></option>
</select>
</div>

<td><input type="text" id="destination"> </td>

For example, if "Samsung" is selected then "1" should be send to the input field and so on. Actually, i do not get any output.

5 Answers 5

1
<select id="source" onchange="copyTextValue()">
    <option value="0">select phone</option>
    <option value="1">samsung</option>
    <option value="2">something</option>
</select>

The id="source" attribute should be in <select> element, also change onselect to onchange and move it to <select> element too.

Codepen: https://codepen.io/anon/pen/WVxLpz

Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve this by setting the listener to the select element and then query the selected option value.

I made a minimal example with two brands:

<script>
function copyTextValue() {
    var e = document.getElementById("select");
    var val = e.options[e.selectedIndex].value;
    document.getElementById("destination").value = val;
}
</script>

<div>
<select onchange="copyTextValue()" id="select">
    <option value="0">select phone</option>
    <option value="1">Brand 1</option>
    <option value="2">Brand 2</option>
</select>
</div>

<td><input type="text" id="destination"> </td>

Comments

0

one of the simple thing you have to observe here is that you have to capture the event when the dropdown is selected, and pass the current dropdown reference to your method.

<script>
function copyTextValue(selectedOption) {
if(selectedOption.selectedIndex <= 0){
document.getElementById("destination").value = '';
return;
}

 var selectedOptionValue = selectedOption.value;
 document.getElementById("destination").value = selectedOptionValue;
}
</script>
<div>
<select onChange="copyTextValue(this);">
    <option value="0">select phone</option>
    <option value="1">select first phone</option>
    <option value="2">select second phone</option>
    <option value="3">select third phone</option>
    <option value="4">select fourth phone</option>

</select>
</div>

<td><input type="text" id="destination"> </td>

here you are also trying to avoid to pass any value to the textbox when the first element is selected. @kryptur's answer is also correct, but this is simpler.

Comments

0

You're using Thymeleaf. For these you to create a form to send you data to the server.

Follow this link for documentation for your exact problems.

https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form

As Frameworks like Thymeleaf usually store state on the server which means you update server first - and then your UI gets updated.

Comments

0

what value return is the value of the select field what you need to do is get the text of selected option i.e

function copyTextValue() {
var selectNode = document.getElementById("source");
enter code here
  document.getElementById("destination").value = 
  selectNode .options[selectNode .selectedIndex].textContent;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.