0

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

1
  • a) You didn't really tell your problem. b) You could just search for this stuff easily. Commented Oct 4, 2012 at 18:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This isn't the correct way to select the element, you should use this as the context for getting the :selected element, $(':selected', this) - read here
1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.