Matt Matt - 1 year ago 76
jQuery Question

Get selected option from select element

I am trying to get the selected option from a dropdown and populate another item with that text, as follows. IE is barking up a storm and it doesn't work in Firefox:

$('#ddlCodes').change(function() {
$('#txtEntry2').text('#ddlCodes option:selected').text();

What am I doing wrong?

Answer Source

Here's a short version:

$('#ddlCodes').change(function() {

karim79 made a good catch, judging by your element name txtEntry2 may be a textbox, if it's any kind of input, you'll need to use .val() instead or .text() like this:


For the "what's wrong?" part of the question: .text() doesn't take a selector, it takes text you want it set to, or nothing to return the text already there. So you need to fetch the text you want, then put it in the .text(string) method on the object you want to set, like I have above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download