Latest web development tutorials

jQuery val () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

Settings <input> field values:

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

try it"

Definition and Usage

val () method returns or sets the property value of the selected element.

When used for the return value:
This method returns the first element value matches the value of the property.

When used settings:
This method sets the value of all matched elements of value attributes.

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


grammar

Returns the value property:

$(selector).val()

Set the value property:

$(selector).val(value)

Set the value property by function:

$(selector).val(function(index,currentvalue))

参数 描述
value 必需。规定 value 属性的值。
function(index,currentvalue) 可选。规定返回要设置的值的函数。
  • index- 返回集合中元素的 index 位置。
  • currentvalue- 返回被选元素的当前 value。


Examples

More examples

Returns the value property
How to return value of the first matched element value attribute.

Use the function to set the property value
Use the function to set the value of the property value.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods