jQuery val() attribute

The val() attribute returns or sets the value attribute of the selected element. It gets the current value of the first element in the set of selected elements and it sets the value of the value attribute to all selected elements. This is basically used in form elements, like - input, select and textarea.

Syntax of val() attribute


The selector is the element from which value to be get or set. The value is the required field. It specifies the value of the value attribute.


<select name="animal" id="animal"> 

<p>The selected animal is <span id="getvalue">Deer</span></p>


Output of the above code

The selected animal is Deer

In the above example, we take the value of the selected animal through jQuery and sets in the paragraph.