jQuery .val() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Attribute methods

What You’ll Learn

The .val() method gets or sets the current value of form elements. This tutorial covers getter and setter overloads (value and callback since 1.4), single and multi-select arrays, comparisons with .prop('value') and .attr('value'), the change event caveat, and five official jQuery demos.

01

Getter

First element

02

Setter

All elements

03

Multi-select

Array return

04

Callback

Since 1.4

05

vs .prop()

Form values

06

Since 1.0

Core API

Introduction

Forms are everywhere on the web — login boxes, search bars, dropdown menus, and comment fields. When JavaScript needs to read what the user typed or programmatically fill in a field, jQuery provides .val().

Available since jQuery 1.0, .val() is the dedicated API for form element values on input, textarea, and select elements. It handles edge cases that raw property access misses — especially multi-select dropdowns that return an array of selected option values.

Use .prop() for boolean form state like checked and disabled, and .val() for the text or option value the user sees and submits. After setting a value, call .trigger("change") if you need change event handlers to run.

Understanding the .val() Method

As a getter, .val() returns the current value of the first matched element — a string for text inputs, a number in some cases, or an array for multi-select elements. On an empty jQuery collection it returns undefined.

As a setter, .val(value) or .val(function) sets the value on every matched element and returns the jQuery object for chaining. Passing an array checks or selects matching options, checkboxes, and radio buttons inside a form.

💡
Beginner Tip

$("#email").val() reads the typed email. $("#email").val("") clears it on every matched input. For a multi-select, $("#tags").val() returns ["js", "css"] when two options are selected.

📝 Syntax

jQuery .val() supports three forms:

Get value — since 1.0

jQuery
.val()
  • Returns the value of the first matched element.
  • Multi-select returns an array; empty collection returns undefined.

Set value — since 1.0

jQuery
.val( value )
  • value — string, number, or array of strings for multi-select / checkbox groups.
  • Sets the value on every matched element.

Callback setter — since 1.4

jQuery
.val( function( index, currentValue ) {
  return currentValue.toUpperCase();
})

Return values

  • Getter: String, Number, Array, or undefined.
  • Setter: the original jQuery object (for chaining).

NOTE: change event is not fired

Setting a value with .val() does not dispatch the change event. Trigger it manually when needed:

jQuery
$( "#country" ).val( "US" ).trigger( "change" );

Reading checked inputs and selects

jQuery
$( "select#foo option:checked" ).val();
$( "select#foo" ).val();
$( "input[type=checkbox][name=bar]:checked" ).val();
$( "input[type=radio][name=baz]:checked" ).val();

⚡ Quick Reference

GoalCode
Read text inputvar v = $("input#name").val()
Clear all matched inputs$("input.search").val("")
Set select option$("#country").val("US")
Multi-select array$("#tags").val(["js", "css"])
Transform with callback$("input").val(function(i, val){ return val.trim(); })
Fire change after set$("#x").val("y").trigger("change")

📋 .val() vs .prop('value') vs .attr('value') vs .prop('checked')

Four APIs for form-related data — use the right one.

.val()
form text

Get/set input, textarea, and select values — preferred API

.prop('value')
raw prop

Direct property access — lacks multi-select array logic

.attr('value')
HTML attr

Initial markup default — not live typed text

.prop('checked')
boolean

Live checked state — not the value attribute string

Examples Gallery

Examples 1–5 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for reading form values.

Example 1 — Official Demo: Single and Multi-Select Values

Display the selected value from a dropdown and an array from a multi-select on every change event.

jQuery
function displayVals() {
  var singleValues = $( "#single" ).val();
  var multipleValues = $( "#multiple" ).val() || [];
  $( "p" ).html(
    "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " )
  );
}
$( "select" ).on( "change", displayVals );
displayVals();
Try It Yourself

How It Works

Single select returns one string. Multi-select returns an array — use .join() to display it. Since jQuery 3.0, no selection returns [] instead of null.

Example 2 — Official Demo: Live Input Value on Keyup

Read the current text as the user types and mirror it in a paragraph.

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

How It Works

$(this).val() inside the handler reads the live typed text. .trigger("keyup") runs the handler once on load so the paragraph reflects the initial value.

📈 Practical Patterns

Setting values, callback transforms, and batch form updates.

Example 3 — Official Demo: Set Input Value From Button Text

Click a button to copy its label into an input field.

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

How It Works

The setter .val(text) updates every matched input. Remember: this does not fire change — add .trigger("change") if listeners depend on it.

Example 4 — Official Demo: Callback Setter Uppercases on Blur

Transform each input’s value when the user tabs away.

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

How It Works

Since jQuery 1.4, the callback form receives (index, currentValue). Return the new value; return undefined to leave unchanged. Useful for trimming, formatting, or normalizing user input.

Example 5 — Official Demo: Set Select, Multi-Select, Checkboxes, and Radio

Pass strings and arrays to select matching options and check matching inputs.

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

How It Works

When you pass an array to .val() on a mixed collection of inputs inside a form, jQuery checks or selects elements whose value matches an array entry and unchecks the rest.

🚀 Common Use Cases

  • Read search queryvar q = $("#search").val() before Ajax lookup.
  • Clear form on reset$("form input, form textarea").val("").
  • Pre-fill edit forms$("#title").val(record.title) after loading JSON.
  • Populate dropdown from API — build options, then $("#country").val(user.country).
  • Trim on submit$("input").val(function(i,v){ return v.trim(); }).
  • Serialize companion — pair with .serialize(); both read current .val() state.

🧠 How .val() Reads and Writes Form Values

1

Match form elements

input, textarea, or select nodes in the jQuery set.

Input
2

Getter or setter?

No args = read first element; value or callback = write all.

Mode
3

Resolve element type

Text, select-one, select-multiple, checkbox, or radio logic.

Logic
4

Value returned or set

String, array, or jQuery object — no automatic change event on set.

📝 Notes

  • Available since jQuery 1.0 — callback setter since 1.4.
  • Getter reads the first element only; setter updates every match.
  • Multi-select returns an array; jQuery 3.0+ returns [] when nothing is selected.
  • Setting .val() does not fire change — call .trigger("change") when needed.
  • Prefer .val() over .prop("value") for form text and select options.
  • Use .prop("checked") for boolean checked state, not .val() alone.
  • On empty collections, getter returns undefined.

Browser Support

.val() has been part of jQuery since 1.0+. Callback setter arrived in 1.4+. Multi-select empty-array behavior changed in jQuery 3.0 (was null, now []). Works in all browsers supported by your jQuery build.

jQuery 1.0+

jQuery .val()

Universal form-value API across jQuery 1.x, 2.x, 3.x, and 4.x. jQuery normalizes select, checkbox, and radio value reading better than raw element.value in legacy code. Textarea newline handling has a documented valHook workaround for carriage-return edge cases.

100% With jQuery loaded
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: Default choice for reading and writing form field values in jQuery projects.

Conclusion

The jQuery .val() method is the standard way to read and write form element values. Use the getter to read the first matched input, textarea, or select — including arrays from multi-select dropdowns. Use the setter or callback form to update every matched element in one call.

Pair it with .prop() for boolean state like checked and disabled. After programmatic changes, call .trigger("change") when event handlers must run. Next up: stripping HTML attributes with .removeAttr().

💡 Best Practices

✅ Do

  • Use .val() for input, textarea, and select text/values
  • Call .trigger("change") after programmatic .val() sets
  • Use callback form to trim or normalize before submit
  • Check selector matched elements before reading .val()
  • Combine with .prop() for checked/disabled state

❌ Don’t

  • Use .attr("value") expecting live typed text
  • Assume .val() sets fire the change event
  • Read .val() on empty collections without checking length
  • Use .prop("value") when .val() handles your case
  • Confuse .val() on checkbox with .prop("checked")

Key Takeaways

Knowledge Unlocked

Five things to remember about .val()

The form-value API — read first, write all.

5
Core concepts
set 02

Setter

All matches

Write
[] 03

Multi-select

Array value

Select
fn 04

Callback

Since 1.4

Transform
! 05

No change

Trigger manually

Event

❓ Frequently Asked Questions

.val() gets or sets the current value of form elements — input, textarea, and select. As a getter it returns the value of the first matched element (string, number, or array for multi-select). As a setter it updates every matched element. Available since jQuery 1.0; callback setter since 1.4.
.val() is jQuery's dedicated form-value API and handles selects, checkboxes, and radios consistently — including array values for multi-select. .prop('value') reads the raw value property on inputs but lacks .val()'s form-specific logic. Prefer .val() for form text and selection state.
For a select element with the multiple attribute, .val() returns an array of each selected option's value. Since jQuery 3.0, an empty selection returns an empty array []; before 3.0 it returned null.
No. Setting a value with .val() (or the native value property) does not fire the change event. Call .trigger('change') after .val(newValue) if you need change handlers to run.
undefined. When no elements match the selector, the getter form .val() returns undefined — always verify your selector matched elements before reading.
Use $("input[name=foo]:checked").val() or read .val() directly from a select. For boolean checked state (not the value attribute), use .prop('checked') or .is(':checked') instead.
Did you know?

Before jQuery unified form handling, developers mixed element.value, element.selectedIndex, and manual checkbox loops. .val() wraps all of that into one API — and the array setter lets you check multiple boxes or select several dropdown options with a single call, deselecting anything whose value is not in the array.

Next: jQuery .removeAttr() Method

Remove HTML attributes from matched elements — the markup cleanup API.

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

6 people found this page helpful