val() method in jQuery

val() in jQuery

Using this method we can get the value of the first element in the set of matched elements.

This method does not cause the dispatch of the change event if setting the values.

Syntax 1 - Get the current value of the first element in the set of matched elements.

$(selector).val()

Syntax 2 - Set the value of each element in the set of matched elements.

$(selector).val(value)

  • value - It represents a string of text, a number, or an array of strings corresponding to the value of each matched element to set as selected/checked.
Syntax 3 - A function returning the value to set

$(selector).val(function(index, currentValue))

  • index - Index position of the element in the set.
  • currentValue - The old value.

Example 1 - get textbox value

Try Yourself

Example 2 - set textbox value

Try Yourself

Example 3 - get old value and set value

Try Yourself

Example 4 - set and get textarea value

Try Yourself

Example 5 - set and get drop down value

Try Yourself

Example 6 - get selected radio button value

Try Yourself

Example 7 - get checkbox value

Try Yourself