I have a drop down that has an \'ID, Name\' Pair.
Example
Jon Miller
Jim Smith
Jen Morsin
Jon MIller has ID of 101
Jim Smith has ID of 10
Try this:
var text = $('#YourDropdownId').find('option:selected').text();
$('.leave_response').on('change', function() {
var responseId = $(this).val();
console.log(responseId);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
<select class="leave_response" name="leave">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</td>
If you're using a <select>
, .val() gets the 'value' of the selected <option>
. If it doesn't have a value
, it may fallback to the id
. Put the value you want it to return in the value
attribute of each <option>
Edit: See comments for clarification on what value
actually is (not necessarily equal to the value
attribute).
This has worked for me!
$('#selected-option option:selected').val()
Hope this helps someone!
var sal = $('.selectSal option:selected').eq(0).val();
selectSal
is a class.
As has been pointed out ... in a select
box, the .val()
attribute will give you the value of the selected option. If the selected option does not have a value attribute it will default to the display value of the option (which is what the examples on the jQuery documentation of .val show.
you want to use .text()
of the selected option:
$('#Crd option:selected').text()