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
$(selector).val(value)
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.
Example
<select name="animal" id="animal">
<option>Deer</option>
<option>Dog</option>
<option>Lion</option>
</select>
<p>The selected animal is <span id="getvalue">Deer</span></p>
<script>
$(document).ready(function(){
$('select#animal').change(function(){
$('span#getvalue').html($("#animal").val());
});
});
</script>
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.