English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The prop() method gets or sets the properties and values of the selected elements.
When using the prop() methodGetwhen setting the attribute value, it will returnThe first selected elementvalue.
Use the prop() methodSetwhen setting the attribute value, it will beAll selected elementsSet one or more properties/value pairs.
To get or set HTML attributes, please useattr()Method.
To remove an attribute, useremoveProp()Method.
Get the value of the property:
$("selector").prop(property)
Set property and value:
$("selector").prop(property, value)
Set multiple properties and values:
$("selector").prop({property:value, property:value, ...})
Use a function to set the property and value
$("selector").prop(property, function(index, currentValue))
Get the value of the checked property of the checkbox:
$("input:checkbox").change(function(){ $("strong").text($(this).prop("checked")); });Test to see‹/›
Set the value of the checked property of the checkbox:
$("button").click(function(){ $("input:checkbox").prop("checked", true); });Test to see‹/›
Disable all checkboxes on the page:
$("document").ready(function(){ $("input:checkbox").prop("disabled", true); });Test to see‹/›
The difference between prop() and attr() may be important in certain situations.
The prop() method provides a way to explicitly retrieve the attribute value, while attr() retrieves the attribute.
The following example shows the difference between prop() and attr():
$("input:checkbox").change(function(){ $(this).prop("checked"); $(this).attr("checked"); });Test to see‹/›
Parameter | Description |
---|---|
property | Specify the name of the attribute |
value | Specify the value of the attribute |
function(index, currentValue) | Specify a function that returns the attribute value to be set
|