Javascript examples for jQuery Method and Property:prop
The prop() method sets or returns properties and values of the selected elements.
When returning the property value, it returns the value of the FIRST matched element.
When setting property values, it sets one or more property/value pairs for the set of matched elements.
Parameter | Description |
---|---|
property | the name of the property |
value | the value of the property |
function(index,currentvalue) | a function that returns the property value to set |
The following code shows how to Add and remove a property named "color":
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var $x = $("div"); $x.prop("color", "red"); $x.append("color: " + $x.prop("color")); $x.removeProp("color"); $x.append("<br>color: " + $x.prop("color")); });// w ww .j a va2s . c o m }); </script> </head> <body> <button>Add and remove a property</button><br><br> <div></div> </body> </html>