October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Get an Input Value with jQuery

Use jQuery's .val() getter to read the current value of a form control. Learn what it returns for different controls and how to collect multiple values.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call .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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • 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
Sale
JavaScript and jQuery: Interactive Front-End Web Development
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$("#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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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

SaleBestseller No. 1
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 2
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript Jquery; Introduces core programming concepts in JavaScript and jQuery; Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
$22.75

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.