davidutzu_s
davidutzu_s

Reputation: 11

Populate input text with drop down select option text

I have a classified ads site and I want in the post ads form when a user select some options from drop down list the text from that option to populate automatically an input field. I managed to get the values for that select using .val, but when i use .text the input field is filled with all options available in the select. I have a demo to understand better jsfiddle.net

Upvotes: 0

Views: 1874

Answers (2)

Erwin
Erwin

Reputation: 4817

If you only want the selected item text you have to use the :selected selector like this:

var selected_item = $(":selected", this).text()

See: jsfiddle.net

More info about the :selected Selector

Upvotes: 2

Ashwin Chandran
Ashwin Chandran

Reputation: 1467

To get the selected value of a dropdown use the val() instead of text(). Change:

var selected_item = $(this).text()

to

 var selected_item = $(this).val()

Upvotes: 1

Related Questions