How can I get the selected value of a drop-down list with jQuery?

jQueryWeb DevelopmentFront End Technology

With jQuery, it’s easy to get selected text from a drop-down list. This is done using the select id. To change the selected value of a drop-down list, use the val() method.

Example

You can try to run the following code to learn how to change the selected value of a drop-down list. Here, we have a default select option first, but the alert shows the second option, which have been changed using the val method():

Live Demo

<html>

<title>jQuery Selector</title>

<script>
$(document).ready(function() {$("#button1").click(function(){
$("#myselection").val('2'); alert($("#myselection option:selected").text() );
});
});
</script>

<body>

<div>
<p>The selected value:</p>
<select id="myselection">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
</div>
<button id="button1">Click</button>

</body>
</html>
Published on 17-Dec-2017 19:45:57