SyntaxStudy
Sign Up

.val()

method

Gets or sets the value of form elements like inputs, selects, and textareas.

Syntax

$(selector).val() $(selector).val(newValue)

Example

javascript
// Get input value
const name = $('#name').val();
const selected = $('#country').val();

// Set value
$('#name').val('Alice');
$('#country').val('uk');

// Clear field
$('input').val('');

// On input change
$('#search').on('input', function() {
    console.log($(this).val());
});