Get selected option from select element
JqueryHtml SelectJquery Problem Overview
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?
Jquery Solutions
Solution 1 - Jquery
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.
Solution 2 - Jquery
Try this:
$('#ddlCodes').change(function() {
var option = this.options[this.selectedIndex];
$('#txtEntry2').text($(option).text());
});
Solution 3 - Jquery
Here is a shorter version that should also work:
$('#ddlCodes').change(function() {
$('#txtEntry2').text(this.val());
});
Solution 4 - Jquery
With less jQuery:
<select name="ddlCodes"
onchange="$('#txtEntry2').text(this.options[this.selectedIndex].value);">
this.options[this.selectedIndex].value
is plain JavaScript.
(Source: German SelfHTML)
Solution 5 - Jquery
Try following code
$('#ddlCodes').change(function() {
$('#txtEntry2').val( $('#ddlCodes :selected').text() );
});
Solution 6 - Jquery
To know number of selected elements use
$("select option:selected").length
To Get the value of all selected elements use
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
Solution 7 - Jquery
This worked for me:
$("#SelectedCountryId_option_selected")[0].textContent
Solution 8 - Jquery
The first part is to get the element from the drop down menu which may look like this:
>
To capture via jQuery you can do something like so:
> $('#cycles_list').change(function() { var mylist = document.getElementById("cycles_list"); iterations = mylist.options[mylist.selectedIndex].text; });
Once you have stored the value in your variable, the next step would be to send the information stored in the variable to the form field or HTML element of your choosing. It may be a div p or custom element.
> i.e. <p></p> OR <div></div>
You would use:
> $('p').html(iterations); OR $('div').html(iterations);
If you wish to populate a text field such as:
> <input type="text" name="textform" id="textform"></input>
You would use:
> $('#textform').text = iterations;
Of course you can do all of the above in less steps, I just believe it helps people to learn when you break it all down into easy to understand steps... Hope this helps!
Solution 9 - Jquery
Using the selectedOptions property (HTML5) you can get the selected option(s)
document.getElementbyId("id").selectedOptions;
With JQuery can be achieved by doing this
$("#id")[0].selectedOptions;
or
$("#id").prop("selectedOptions");
The property contains an HTMLCollection array similitar to this one selected option
[<option value="1">ABC</option>]
or multiple selections
[<option value="1">ABC</option>, <option value="2">DEF</option> ...]
Solution 10 - Jquery
The above would very well work, but it seems you have missed the jQuery typecast for the dropdown text. Other than that it would be advisable to use .val()
to set text for an input text type element. With these changes the code would look like the following:
$('#txtEntry2').val($('#ddlCodes option:selected').text());
Solution 11 - Jquery
if you have this already and use jquery this will be your answer:
$($(this)[0].selectedOptions[0]).text()
Solution 12 - Jquery
Given this HTML:
<select>
<option value="0">One</option>
<option value="1">Two</option>
</select>
Select by description for jQuery v1.6+:
var text1 = 'Two';
$("select option").filter(function() {
//may want to use $.trim in here
return $(this).text() == text1;
}).prop('selected', true);