Get selected parameter data using Jquery or javascript
I need to get the data of a selected parameter whose parameter value is known. I have a parameter value selected and I want the data to be wrapped between the option.
For example, in the following list:
<select name="oi_report_contact[sex]" id="oi_report_contact_sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Other</option>
</select>
I have a value of 1, I need to get the Male data via JQuery or Javascript.
Note: $('#oi_report_contact_sex').val();
Will give 1, not masculine when 1 is selected.
+3
Sachin Prasad
source
to share
4 answers
You just need to call
var content = $('#oi_report_contact_sex option:selected').html();
to get the inner content of the selected option.
+6
j_freyre
source
to share
You can use the method .text()
to get the text value. Like this .
$('#oi_report_contact_sex').on('change', function () {
alert($('#oi_report_contact_sex').val());
alert($('#oi_report_contact_sex option:selected').text());
});
+4
hjpotter92
source
to share
$("#oi_report_contact_sex").find('option:selected').text();
+2
MAAAAANI
source
to share
You may try:
$("#oi_report_contact_sex option[value='" + $("#oi_report_contact_sex").val() + "']").text()
jsFiddle link: http://jsfiddle.net/ARBb2/1/
0
malkassem
source
to share