How to get selected option value in jquery?

8 answer(s)
Answer # 1 #

Modern twist: With React? Use ref.current.value. But jQuery bridges old codebases.

[4 Month]
Answer # 2 #

Quick: $('#id').val(). Boom.

[5 Month]
Answer # 3 #

Dev with 10+ yrs: Full snippet: javascript$(document).ready(function(){ $('#btn').click(function(){ var selected = $('#mySelect option:selected').val(); alert(selected); });}); Handles defaults, updates on change. Vs. text: .text(). Edge: Empty select? Null. jQuery 3.7 stable. GeeksforGeeks: jQuery Value

[5 Month]
Answer # 4 #

Deep: For dynamic options, use .find(':selected').val(). AJAX load? Bind after. Common bug: No quotes on ID. Vanilla alt: document.getElementById('id').value. Stack Overflow: Selected Value

[4 Month]
Answer # 5 #

Debug tip: Chrome console—paste code, see output.

[4 Month]
Answer # 6 #

Form validation: If(!$('#select').val()) { error; } Multi-select: $('select[multiple]').val().push to array.

[4 Month]
Answer # 7 #

Legacy support: IE8+ fine. Tutorialspoint: jQuery Select

[4 Month]
Answer # 8 #

JS newbie? jQuery magic: For