.val()

.val()

  • 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)

Reading Values

  • To read (or get) the value of elements using jQuery you need to use the .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()

JS Bin on jsbin.com


Writing Values

  • To write or update the value of elements using jQuery you need to use the .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()

JS Bin on jsbin.com