0

I have this script, the idea is to select from the drop down list and then display the selected value in a text field (HTML Form). When i make the selection, I do not get the value displayed in the text field, please help.

<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction()">
    <option value="Audi">Audi
    <option value="BMW">BMW
    <option value="Mercedes">Mercedes
    <option value="Volvo">Volvo
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<input type="text" id="demo" onload="myFunction()" >



<script>
    function myFunction() {
        var x = document.getElementById("mySelect").value;
        document.getElementById("demo").innerHTML = x;
    }

</script>

3
  • The element with id="demo" is hidden. Commented Feb 1, 2019 at 14:53
  • 1
    You commented out your demo element... Commented Feb 1, 2019 at 14:53
  • I had commented it out to call the variable in the <input element Commented Feb 2, 2019 at 7:57

3 Answers 3

2

Uncomment <!--<p id="demo"></p>--> and it works fine.

  function myFunction() {
        var x = document.getElementById("mySelect").value;
        document.getElementById("demo").innerHTML = x;
    }
<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction()">
    <option value="Audi">Audi
    <option value="BMW">BMW
    <option value="Mercedes">Mercedes
    <option value="Volvo">Volvo
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<p id="demo"></p>

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

1 Comment

I had commented it out to call the variable in the <input element. I have edited my question to show exactly what i wanted.
0

<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction()">
    <option value="Audi">Audi
    <option value="BMW">BMW
    <option value="Mercedes">Mercedes
    <option value="Volvo">Volvo
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>


<p id="demo"></p>
<!--<p id="demo"></p>-->



<script>
    function myFunction() {
        var x = document.getElementById("mySelect").value;
        document.getElementById("demo").innerHTML = x;
    }

</script>

You had not created a text field or any other element with the id "demo".

1 Comment

I have this line right after the closing tag of script, <input type="text" id="demo" value="">
0

you can use, the following script that is in jquery to do this job,,

    $(function(){
    var select_value = $('#mySelect').find(":selected").val();
     $("#demo").text(select_value);
     }); 

//in javascript, this might work

function myFunction() {

    var x = document.getElementById("mySelect").find(":selected").value;
    document.getElementById("demo").innerHTML = x;
    document.getElementById("id_of_text_field_input").value = x;

}

2 Comments

This works, but in a paragraph element, but i want it to be displayed in a textfield
you can use following code for this job, function myFunction() { var x = document.getElementById("mySelect").find(":selected").value; document.getElementById("id_of_text_field_input").value = x; }

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.