Home » jQuery val()

jQuery val()

There are two usage of jQuery val() method.

  • It is used to get current value of the first element in the set of matched elements.
  • It is used to set the value of every matched element.

Syntax:

It is used to get value.

It is used to set value.

It is used to set value using function.

Parameters of jQuery val() method

Parameter Description
Value It is a mandatory parameter. It is used specify the value of the attribute.
Function (index, currentvalue) It is an optional parameter. It is used to specify a function that returns the value to set.

jQuery val() example

The val() method is primarily used to get the values of form elements. This method doesn’t accept any arguments. This method returns a NULL when no option is selected and it returns an array containing the value of each selected options in the case of one or more selection.

Let’s see the example of val() method.

Test it Now

Output:

Let's see example of jQuery val() method with single and multiple select boxes.

Test it Now

Output:

jQuery val(value) example

This method is used to set a string of text, a number, an array of strings corresponding to the value of each matched element. This method facilitates you to set the value by passing in the function.

Let's see the example of val(value) method.

Test it Now

Output:

Name:

Next TopicjQuery css()

You may also like