Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Call .val() with no argument on a jQuery selection: const value = $("#myInput").val(); This reads the current value of the first matched form control.
Get the value from one input
Use a selector that identifies the input you want, such as an ID:
const value = $("#myInput").val();
The no-argument form is the getter. It returns the current value of the first element in the matched set, so a specific selector is preferable when you intend to read one particular control. For example, $("input").val() reads only the first matched input.
Read values from different form controls
.val() is intended for form controls, including inputs, textareas, and selects. Its result depends on the control:
#1 Best Overall
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
- Input or textarea: the current value.
- Single select: the selected option’s underlying value.
- Multiple select: an array of selected values. In jQuery 3.0 and later, a multiple select with no selected options returns an empty array; earlier versions returned
null.
If you need the visible label of a selected option rather than the value submitted by the form, read the selected option’s text:
const label = $("#mySelect option:selected").text();
Get every matching input’s value
A getter does not return a list of values for all matched elements; it reads the first match. To collect values from every matching input, iterate over the selection:
Rank #2
- JavaScript Jquery
- Introduces core programming concepts in JavaScript and jQuery
- Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
const values = $(".answer").map(function () {
return $(this).val();
}).get();
Narrow the selector as needed so the collection contains only the controls you want. For the complete set of values submitted by a form, jQuery also provides serialize() and serializeArray().
Set a value instead of getting it
Pass an argument to .val() to set a value. A setter updates every element in the matched set; it does not automatically dispatch a change event.
$("#myInput").val("New value");
// Run change handlers as well, if the application requires it:
$("#myInput").val("New value").trigger("change");
Why does .val() return undefined?
An empty jQuery selection returns undefined. Check that the selector matches the intended element and that the code runs after the element is available in the page.
Use .val(), not .text(), for input values
For an input or textarea, use .val() to read the current form value. .text() reads text content and is not the input-value getter. For a select, also keep the distinction clear: .val() returns the selected option’s value, while the option’s text is its visible label.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




