Easy Tutorial
❮ Misc Removedata Misc Index ❯

jQuery val() Method

jQuery HTML/CSS Methods

Example

Set the value of an <input> field:

$("button").click(function(){
    $("input:text").val("Glenn Quagmire");
});

Definition and Usage

The val() method returns or sets the value attribute of the selected elements.

When used to return a value: This method returns the value attribute of the first matched element.

When used to set a value: This method sets the value attribute for all matched elements.

Note: The val() method is typically used with HTML form elements.


Syntax

Return the value attribute:

Set the value attribute:

Set the value attribute using a function:

Parameter Description
value Required. Specifies the value of the value attribute.
function(index, currentvalue) Optional. Specifies a function that returns the value to set. index - Returns the index position of the element in the set.<br><br>currentvalue - Returns the current value of the selected element.

More Examples

Return the value attribute

Set the value attribute using a function


jQuery HTML/CSS Methods

❮ Misc Removedata Misc Index ❯