jQuery .val(): Read and Set Form Field Values
jQuery’s .val() reads or writes the current value of an input, select, or textarea. Calling it with no arguments reads the first match. Passing a value updates every match and leaves any change handlers waiting for an event.
Read text, select, and checkbox values
In the official jQuery .val() documentation, an ordinary text input returns a string. A multiple select returns an array of selected option values. With no selected options, jQuery 3.0 and later return []. Reading an empty jQuery collection returns undefined.
Calling .val() on a checkbox reads its value. Use .prop("checked") from jQuery .prop() for its checked state. To collect values from multiple checked boxes, filter with :checked and use .map() and .get().
| Example call | Return value |
|---|---|
$("#name").val() |
Current text as a string |
$("#topics").val() |
Array of selected values |
$updates.val() |
Value string of the first checkbox |
$updates.filter(":checked").map(…).get() |
Array of checked checkbox values |
Set new values or clear a selection
Set a text input with a string and a multiple select with an array of strings matching the options’ value attributes. On a checkbox collection, an array checks boxes with matching values and clears the other boxes.
const name = $("#name").val();
$("#name").val("Alex Kim");
$("#topics").val(["html", "css"]);
Use .val("") to clear a text input and .val([]) to clear a multiple selection. Read the value back if you need to confirm that the option values matched.
Test value changes and events in a browser
Save the code as a UTF-8 file named example.html and open it in a browser. It needs an internet connection to load jQuery 4.1.0. The name starts with a default value, and no topics or notifications are selected.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inspect input values with jQuery .val()</title>
<style>
body { max-width: 32rem; margin: 1rem auto; padding: 0 1rem; font: 1rem/1.5 system-ui; }
input, select, button { font: inherit; }
#name, #topics { display: block; box-sizing: border-box; max-width: 100%; margin-bottom: .6rem; }
fieldset { margin: .8rem 0; }
#output { white-space: pre-wrap; overflow-wrap: anywhere; }
</style>
<script src="https://code.jquery.com/jquery-4.1.0.min.js"></script>
</head>
<body>
<label for="name">Name</label>
<input id="name" value="Initial name">
<label for="topics">Topics</label>
<select id="topics" multiple>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="jquery">jQuery</option>
</select>
<fieldset>
<legend>Notifications</legend>
<label><input type="checkbox" name="updates" value="email"> Email</label>
<label><input type="checkbox" name="updates" value="sms"> Text message</label>
</fieldset>
<button type="button" id="fill">Set values</button>
<button type="button" id="notify">Notify change</button>
<p>Last values read</p>
<pre id="output" aria-live="polite"></pre>
<script>
$(function () {
const $name = $("#name");
const $topics = $("#topics");
const $updates = $('input[name="updates"]');
function showValues() {
const checked = $updates.filter(":checked").map(function () {
return this.value;
}).get();
const values = {
["Name"]: $name.val(),
["Topics"]: $topics.val(),
["Notifications"]: checked
};
$("#output").text(JSON.stringify(values, null, 2));
}
$name.add($topics).add($updates).on("change", showValues);
$("#fill").on("click", function () {
$name.val("Alex Kim");
$topics.val(["html", "css"]);
$updates.val(["email"]);
});
$("#notify").on("click", function () {
$topics.trigger("change");
});
showValues();
});
</script>
</body>
</html>
Click Set values to enter Alex Kim, select HTML and CSS, and check Email. The summary still displays the previous values. Click Notify change to call .trigger("change") and refresh the summary. Manually changing the selection or a checkbox also refreshes it.
When only the input value should change, call .val(). Trigger an event explicitly on the appropriate field if its change handler should run. See how input and change events work for the difference between reacting as someone types and reacting when the change is committed.
Trim a name with a callback
The callback form of .val() receives each field’s index and current value. Add the following code inside the demo’s ready callback or run it in the developer console after typing a name.
$("#name").val(function (index, value) {
return value.trim();
});
trim() removes spaces at the start and end of the name. Apply it only to fields where those spaces carry no meaning. This call also needs a separate change event if you want the summary to update.
Check initial values and numeric input
After editing the demo, $("#name").val() reads the current name and $("#name").attr("value") reads the initial value in the markup. A placeholder is an input hint and does not become its value. See jQuery .attr() and HTML attributes for working with HTML attributes.
Numeric-looking text is still a string. Before calculating, trim it and check that it is nonempty. Convert with Number() and check the result with Number.isFinite().




