jQuery .val() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Attributes

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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();
});
CallWhat it does
.val()Get value of first element
.val(value)Set value on every match
.val(fn)Set from function(index, oldValue)
Multi-select getArray of values · [] if none (jQuery 3.0+)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
valueUsed in .val(value). Set value on every match
fnUsed in .val(fn). Set from function(index, oldValue)

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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

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() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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