English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery prop() Method

jQuery HTML/CSS Methods

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.

Syntax:

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))

Example

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‹/›

prop() and attr()

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 Value

ParameterDescription
propertySpecify the name of the attribute
valueSpecify the value of the attribute
function(index, currentValue)Specify a function that returns the attribute value to be set
  • Index -Receive the index position of the elements in the group

  • currentValue-Receive the current attribute value of the selected element

jQuery HTML/CSS Methods