← Back to Index

.val()

Get the current value of the first element in the set of matched elements or set the value of every matched element.

Examples

1. Get Value

// <input value="Hello">
const val = $('input').val();

Result

"Hello"

2. Set Value

$('input').val('New Value');

Result

<input value="New Value">

3. Handle Select Multiple

// <select multiple>...</select>
$('select').val(['option1', 'option2']);

Result

// Options with values 'option1' and 'option2' are selected.

4. Set with Function

$('input').val((index, value) => value.toUpperCase());

Result

// "hello" becomes "HELLO"

Parameters

Parameter Type Description
value string | number | array | function The value to set. For multiple selects or checkboxes/radios, pass an array of values. If a function, it is called with (index, currentValue).

Returns: The value for getter, or the original SR object for setter.