Load select text in input

0

Hi, I need help with this code, I want the input to be loaded when the select changes the value. But I can not change anything in select, because it's an adaptation I'm making in Prestashop.

<html>

<head>
  <title>TESTE</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head><body><scripttype="text/javascript">
    function getHTML() {
      var vai = $('#testID option:selected').html();
      alert(vai);
    }
  </script>

  <input id="vai" name="vai" value="" onClick="getHTML(select,textfield)">

  <select id="testID">
    <option value="1">Value1</option>
    <option value="2">Value2</option>
    <option value="3">Value3</option>
    <option value="4">Value4</option>
    <option value="5">Value5</option>
    <option value="6">Value6</option>
  </select>
  <input type="text" value="Get dropdown selected Value" onClick="getHTML();">
</body>

</html>
    
asked by anonymous 23.05.2018 / 13:43

1 answer

1

Place the script after HTML and you can do this with:

$('#testID').change(function() {
    $('#vai').val(this.value);
});

Example:

<html>

<head>
  <title>TESTE</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head><body><inputid="vai" name="vai" value="" onClick="getHTML(select,textfield)">

  <select id="testID">
    <option value="1">Value1</option>
    <option value="2">Value2</option>
    <option value="3">Value3</option>
    <option value="4">Value4</option>
    <option value="5">Value5</option>
    <option value="6">Value6</option>
  </select>
</body>

<script type="text/javascript">
  $('#testID').change(function() {
    $('#vai').val(this.value);
  });
</script>

</html>
    
23.05.2018 / 13:47