Content API

val()

Sets or gets the value of the value attribute for the matched DOM elements.

  • Accepts an argument as a string.
  • If no arguments are passed, it returns a string if a single matched DOM element, and an array of strings if multiple matched DOM elements.
Example:
$('input').val('');
$('input').val(null);
// Removes the value of the 'value' attribute from the <input> element.

$('input ?').val('');
$('input ?').val(null);
// Removes the value of the 'value' attribute from all <input> elements.

$('input').val('book');
// Sets the 'value' attribute to 'book' on the <input> element.

$('input').val();
// 'book'

$('input ?').val();
// ['book', '', '']