Usually I use $(\"#id\").val()
to return the value of the selected option, but this time it doesn\'t work.
The selected tag has the id aioConceptName
For good practice you need to use val()
to get value of selected options not text()
.
<label>Name</label>
<input type="text" name="name" />
<select id="aioConceptName">
<option value="choose">choose io</option>
</select>
You can use
$("#aioConceptName").find(':selected').val();
Or
$("#aioConceptName :selected").val();
I wish that helps ..
You can select using exact selected option : Below will give innerText
$("select#aioConceptName > option:selected").text()
While below will give you value.
$("select#aioConceptName > option:selected").val()
For anyone who found out that best answer don't work.
Try to use:
$( "#aioConceptName option:selected" ).attr("value");
Works for me in recent projects so it is worth to look on it.
You can try to debug it this way:
console.log($('#aioConceptName option:selected').val())
Using jQuery, just add a change
event and get selected value or text within that handler.
If you need selected text, please use this code:
$("#aioConceptName").change(function () {
alert($("#aioConceptName :selected").text())
});
Or if you need selected value, please use this code:
$("#aioConceptName").change(function () {
alert($("#aioConceptName :selected").attr('value'))
});
If you want to grab the 'value' attribute instead of the text node, this will work for you:
var conceptName = $('#aioConceptName').find(":selected").attr('value');