The jQuery prop() method sets or returns attributes or values of the selected elements.
Syntax:
When used to return the property’s value:
$(selector).prop(property)
When used to set the property and value:
$(selector).prop(property,value)
When used to set the property and value by using a function:
$(selector).prop(property,function(index,currentvalue))
When used to set multiple properties and values:
$(selector).prop({property:value, property:value,...})
Property:
- Attribute is a compulsory parameter of the jQuery prop() method, as it specifies the name of the property.
Value:
- Value is also a compulsory parameter of the jQuery prop() method, as it specifies the value of the property to set.
Function:
- It is an optional parameter.
- The function parameter is used to return the property’s value.
Index:
- The index is an argument passed within the function.
- It is used to give an index position to an element in the set.
Currentvalue:
- This parameter is used to provide the current property’s value.
Example:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var ("div"); x.append("The value of the color property: " + x.prop("color","yellow"); x.prop("color")); }); }); </script> </head> <body> <button>Add and remove a property</button><br><br> <div></div> </body> </html>