Load select text into input

Asked

Viewed 219 times

0

Hi I need help with this code, I want the input to be loaded when select change the value. But I can’t change anything in select, because it’s an adaptation I’m doing 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>
  <script type="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>

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>


  <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>
</body>

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

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.