Description
The .val() method returns or sets the value attribute of the selected elements.
Note: This function is mostly used with form elements.
Return the Value Attribute
Returns the value of the value attribute of the FIRST matched element.
v1.0
This method has the form:
.val()
Return Value
This form returns a String, Array object.
Set the Value Attribute
Sets the value of the value attribute for ALL selected elements.
v1.0
This method has the alternate form:
.val(value)
Parameter | Description |
---|---|
value | Specifies the value of the value attribute |
Return Value
This form returns a jQuery object.
Set the Value Attribute Using a Function
Using a function to set the value of the value attribute for all selected elements.
v1.4
This method has the alternate form:
.val(function(index,oldvalue))
Parameter | Description |
---|---|
function(index, oldvalue) |
Specifies a function that returns the value to set.
|
Return Value
This form returns a jQuery object.
Examples
Set the value of an input field:
$("button").click(function(){
$("input:text").
val("John Q. Public");
});