How to Get Text from Selected Option in jQuery

Working with <select> elements in HTML is common, and using jQuery makes it easy to extract the text of the selected option. In this post, we’ll walk through a simple example to achieve this. Example Code
 <select id="mySelect"> 
<option value="1">Option 1</option> 
<option value="2">Option 2</option> 
<option value="3">Option 3</option> 
</select> 

<p id="selectedOption"></p> 
 $(document).ready(function() { $('#mySelect').change(function() { var selectedText = $(this).find('option:selected').text(); $('#selectedOption').text('You selected: ' + selectedText); }); }); 

Final Thoughts

This jQuery code will display the text of the selected option whenever the user changes their selection. It’s a great way to enhance user interaction on your forms. Happy coding!

Leave a Comment