Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

jQuery populating text input with text from dropdown select

I've been looking at some of the answers on here about how to grab input from a dropdown selection and then how to insert into the value of a text input but I'm having trouble with my dropdown selection being saved.

I would like to populate the text input with the text value of the option chosen.

$('#options').change({
  var optionChange = $('#options option:selected').text();
  $('#chosen-input').val(optionChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <select id="options">
    <option value="one">This is Option One</option>
    <option value="two">This is Option Two</option>
    <option value="three">This is Option Three</option>
  </select>
  <input type="text" id="chosen-input" value="Lets Change This" />
</form>

Upvotes: 2

Views: 1209

Answers (1)

Rooster
Rooster

Reputation: 10077

you need to pass in a function, not a generic object.

$('#options').change(function(e){
  var optionChange = $('#options option:selected').text();
  $('#chosen-input').val(optionChange);
});

Upvotes: 4

Related Questions