What is .val() function and what it does?


#1

What is .val() function and what it does?


#2

The .val() method is primarily used to get the values of form elements such as input, select and textarea. - http://api.jquery.com/val/


#3

We may not see it or include it in our source HTML, but when a user enters data into a form field, a value="user text" attribute is created on that control. When the element is selected and becomes a jQuery object, it inherits the .val() method as a means of extracting the value attribute data.

We may also use the method to clear a form field, as well.

$('input').val('');

In general terms, that would make it a getter and a setter depending how we use it. When the argument is empty, it is a getter. When the argument contains a string, even empty, it becomes a setter.


#4

great explanation. Thanks