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?
If you want to get selected option value, you can use $(select element). val() .
Summary. Use the <select> element to create a dropdown list. Use the multiple attribute to create a list that allows multiple selections. Use $_POST to get the selected value of the select element if the form method is POST (or $_GET if the form method is GET ).
Here's a short version:
$('#ddlCodes').change(function() { $('#txtEntry2').text($(this).find(":selected").text()); });
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:
$('#txtEntry2').val($(this).find(":selected").text());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With