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


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


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


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.


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.


great explanation. Thanks