Easy Tutorial
❮ Jquery Ref Traversing Jquery Ajax Get Post ❯

jQuery prop() Method

jQuery HTML/CSS Methods

Example

Add and remove the property named "color":

$("button").click(function(){
    var $x = $("div");
    $x.prop("color","FF0000");
    $x.append("The color property: " + $x.prop("color"));
    $x.removeProp("color");
});

Definition and Usage

The prop() method sets or returns properties and values of the selected elements.

When this method is used to return property values, it returns the value of the first matched element.

When this method is used to set property values, it sets one or more property/value pairs for the set of matched elements.

Note: The prop() method should be used to retrieve property values, such as DOM properties (e.g., selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected).

Tip: To retrieve HTML attributes, use the attr() method instead.

Tip: To remove properties, use the removeProp() method.


Syntax

Return the value of a property:

Set property and value:

Set property and value using a function:

Set multiple properties and values:

Parameter Description
property Specifies the name of the property.
value Specifies the value of the property.
function( index,currentvalue) Specifies a function that returns the property value to set. index - Retrieves the index position of the element in the set.<br><br>currentvalue - Retrieves the current property value of the selected element.

More Examples

Differences Between prop() and attr()


jQuery HTML/CSS Methods

❮ Jquery Ref Traversing Jquery Ajax Get Post ❯