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 jQuery prop() method, as it specifies the name of the property.
Value:
- Value is also a compulsory parameter of 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:
- 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 1
<!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 $x = $("div"); $x.prop("color","red"); $x.append("The value of the color property: " + $x.prop("color")); $x.prop("color","yellow"); $x.append("<br>Now the value of the color property: " + $x.prop("color")); }); }); </script> </head> <body> <button>Add and remove a property</button><br><br> <div></div> </body> </html> |
Try it
Please Share