Search code examples
jqueryfunctiondrop-down-menuonchangeselect-menu

Changing the contents of a text field on change of a dropdown menu


I am trying to use the following to change the value of 5 text boxes each time one of the 5 dropdown menus is changed. For some reason its not working though. I just want to take the value from the dropdown and put it into the text box, I dont need any other info such as the data between and , just the value, im not sure why its not working though :-S . The value from the dropdown will be a number string, such as 458766958

    <script>
    $('#stock1').change(function() {
        $('#stock1T').val($('#stock1 option:selected').data('value'));
    })

    $('#stock2').change(function() {
        $('#stock2T').val($('#stock2 option:selected').data('value'));
    })

    $('#stock3').change(function() {
        $('#stock3T').val($('#stock3 option:selected').data('value'));
    })

    $('#stock4').change(function() {
        $('#stock4T').val($('#stock4 option:selected').data('value'));
    })

    $('#stock5').change(function() {
        $('#stock5T').val($('#stock5 option:selected').data('value'));
    })
    </script>

Solution

  • instead of using data() ,try getting the selected value using val()

          $('#stock1').change(function() {
                  $('#stock1T').val($('#stock1 option:selected').val());
             });