jQuery .val() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .val()?

.val() (since 1.0) gets or sets the current value of form elements — mainly input, select, and textarea. Prefer it over .prop("value") for form text and selection.

Remember
// Get
var name = $("#name").val();
var picks = $("#multi").val(); // array for multi-select

// Set
$("#name").val("Ada");
$("#multi").val(["a", "c"]);

// Callback (since 1.4)
$("input").val(function (i, v) {
  return v.trim();
});
NeedUse
Form field value.val()
Live checked / disabled.prop()
HTML attribute (markup).attr()
Serialize a whole form.serialize()
Listen for user edits.on("change")

How to Use .val()

Select the field, then get, set, or compute the value.

  1. Select the form control(s).
  2. Get: .val() — first match only; empty set → undefined.
  3. Set: .val(value) — string, number, or array for multi-select / checks.
  4. Compute: .val(function (index, old) { return next; }) (since 1.4).
  5. If handlers must run, chain .trigger("change") after setting.
SignatureSinceRole
.val()1.0Get value of first element
.val(value)1.0Set value on every match
.val(fn)1.4Set from function(index, oldValue)
Multi-select get—Array of values · [] if none (jQuery 3.0+)

Pseudocode

Pseudocode
if called as .val():
    if no matched elements: return undefined
    if first element is multi-select:
        return array of selected option values
        // jQuery 3.0+: [] when nothing selected
    else:
        return that element's current value

if called as .val(value):
    for each matched element:
        set its value / selected / checked state from value
    // does NOT fire change — call .trigger("change") if needed

if called as .val(fn):
    for each matched element with index i:
        set value to fn(i, currentValue)

Cheat Sheet

Keep these facts handy while reading form values.

TopicDetail
Primary useinput, select, textarea
Getter scopeFirst element only
Empty collectionundefined
Multi-select (3.0+)Array · empty → []
Set + eventsNo auto change — use .trigger("change")
Array setterSelects options / checks matching values
vs .prop("value")Prefer .val() for forms
Checked stateUse .prop("checked"), not .val()

Live Preview

Get or set the sample input value — same APIs as the examples below.

Sample element: the text input below. Type freely, then run an action.

Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Prefer .val() for form values.

Example 1 — Get Input Value on Keyup

Mirror whatever the user types — official getter pattern.

jQuery
$("input").on("keyup", function () {
  var value = $(this).val();
  $("p").text(value);
}).trigger("keyup");
Try It Yourself

How It Works

  1. Listen. Run on each keyup.
  2. Get. $(this).val() reads the current field value.
  3. Show. Write that string into the paragraph.
  4. Start. .trigger("keyup") shows the initial value.

Example 2 — Set Value from a Button

Pass a string to update every matched input.

jQuery
$("button").on("click", function () {
  var text = $(this).text();
  $("input").val(text);
});
Try It Yourself

How It Works

  1. Click. Read the button label with .text().
  2. Set. $("input").val(text) writes that string.
  3. Scope. Every matched input is updated.
  4. Events. No change fires unless you trigger it.

Example 3 — Single and Multi Select

Single select returns a string; multi-select returns an array.

jQuery
function displayVals() {
  var singleValues = $("#single").val();
  var multipleValues = $("#multiple").val() || [];
  $("#out").text(
    "Single: " + singleValues +
    " | Multiple: " + multipleValues.join(", ")
  );
}

$("select").on("change", displayVals);
displayVals();
Try It Yourself

How It Works

  1. Single. .val() returns the selected option value.
  2. Multi. Returns an array of selected values.
  3. 3.0+. No selection → [] (older jQuery used null).
  4. Guard. || [] keeps older jQuery safe when joining.

Example 4 — Callback Setter

Compute the new value from the old one — e.g. uppercase on blur.

jQuery
$("input").on("blur", function () {
  $(this).val(function (i, val) {
    return val.toUpperCase();
  });
});
Try It Yourself

How It Works

  1. Blur. Run when the field loses focus.
  2. Callback. Receives index and the old value.
  3. Return. The return value becomes the new .val().
  4. Use. Trim, normalize case, or build derived text.

Example 5 — Set Select / Checkbox / Radio with an Array

Pass matching values to select options or check controls in one call.

jQuery
$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check1", "check2", "radio1"]);

$("#out").text(
  "single=" + $("#single").val() +
  ", multi=" + $("#multiple").val().join(",")
);
Try It Yourself

How It Works

  1. String. Sets one select option or text field.
  2. Array. Matches option/input values to check or select.
  3. Radios. Only one value in the group stays selected.
  4. Events. Still no automatic change event.

Edge Cases & Pitfalls

These trip up beginners most often with form values.

events

.val() does not fire change

Programmatic sets skip handlers. Chain .trigger("change") when needed.

multi

Empty multi-select changed in 3.0

jQuery 3.0+ returns []; older versions returned null. Use || [] when supporting both.

boolean

.val() ≠ checked state

Use .prop("checked") or :checked to test whether a box is on. .val() returns the value attribute.

textarea

Newline normalization

Browsers may strip \r from textarea .val(). A $.valHooks.textarea can normalize if you need CRLF.

Browser Support

.val() has been part of jQuery since 1.0. Callback setter since 1.4. Multi-select empty result is [] since 3.0. Works wherever your loaded jQuery build runs.

✓ Stable · jQuery 1.0+

jQuery .val()

Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for consistent multi-select empty arrays.

100% jQuery core
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.val() Universal

Bottom line: Safe for everyday forms. Prefer .val() for field values; use .prop() for live checked/disabled state.

Performance Notes

ApproachCostBest for
One .val() get/setCheapTypical form UI
Array set on a small groupCheapMulti-select / checkbox groups
Reading every keystroke on huge pagesWatchDebounce or target one field

Value access is inexpensive. Keep handlers focused on the fields you need — avoid scanning the whole document on every keyup.

Key Takeaways

  • Role: get/set form values on inputs, selects, and textareas.
  • Multi-select: returns an array · empty is [] in jQuery 3.0+.
  • Events: set does not fire change — trigger it yourself.
  • State: use .prop("checked") for on/off, not .val().

One line: .val() is jQuery’s form-value API — get/set text and selections, then trigger change only when you need it.

Frequently Asked Questions

.val() gets or sets the current value of form elements — input, textarea, and select. The getter returns the first match (string, number, or array for multi-select). The setter updates every matched element. Available since jQuery 1.0; callback setter since 1.4.
.val() is jQuery's form-value API and handles selects, checkboxes, and radios — including array values for multi-select. Prefer .val() for form text and selection. Use .prop() for live boolean state like checked or disabled.
An array of each selected option's value. Since jQuery 3.0, no selection returns []. Before 3.0 it returned null.
No. Call .trigger("change") after .val(newValue) if change handlers must run.
undefined. Check that your selector matched elements before reading.
Use $("input[name=foo]:checked").val(). For whether it is checked (boolean), use .prop("checked") or .is(":checked").

Next: .removeAttr() Method

Learn how to strip HTML attributes from matched elements.

.removeAttr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful