Allows you to either read (get) or write (set) the value of html elements (documentation)
This is mainly used to read and write values of form elements such as text fields (input fields)
.val()
method with nothing between the parentheses
// read the value found in the element .name-field
// notice how nothing is in-between the parentheses
$(".name-field").val();
Example #1 - Reading values using .val()
.val()
and place the new text in-between the parentheses
// Update the value found in .name-field to read "Barney the Purple Dinosaur"
// notice how we place "Barney the Purple Dinosaur" in-between the parentheses
$(".name-field").val("Barney the Purple Dinosaur");
Example #2 - Writing values using .val()