user2911924
user2911924

Reputation: 331

Write text to textbox after dropdown menu has been changed

I am looking to add text to a textbox with the selected text from the dropdown menu and place a ',' after.().

<select>
<option value="1">This</option>
<option value="2">is</option>
<option value="3">test</option>
</select>

Now when you select the option 'is' the textbox gets updated with 'is,'. Now still on the same page you select 'This' the textbox gets updated with 'is, This,'. Reload the page and do it the other way around and you get 'This, is,'.

Hope you understand what I am trying to do here, the will contains values from the database so I cannot know what the values are exactly.

Thanks in advance.

Upvotes: 0

Views: 907

Answers (1)

FercoCQ
FercoCQ

Reputation: 149

I think something like this should work:

<select id="dropdownId">
    <option value="1">This</option>
    <option value="2">is</option>
    <option value="3">test</option>
</select>
<input id="textboxId" type="text" />

<script type="text/javascript">
    $('#dropdownId').on('change', function () {
        $('#textboxId').val($('#textboxId').val() + $('#dropdownId option:selected').text() + ', ');
    });
</script>

Fiddle

Upvotes: 1

Related Questions