jQuery | prop() with Examples
The prop() is an inbuilt method in jQuery which is used to set or return properties and values for the selected elements. When this method is used to return the property value, it returns the value of the FIRST matched element and when this method is used to set property values, it sets one or more property for the selected elements.
Syntax :
$(selector).prop(para1, para2)
Parameters: It accepts two parameters which are specified below-
Return Value: It returns the property and value set for the property for the selected element.
jQuery code to show the working of prop() method:
<html> <head> <script </script> <!-- jQuery code to show thre working of this method --> <script> $(document).ready(function(){ $("button").click(function(){ var $x = $("p"); $x.prop("color", "green"); $x.append(" Property is color and its value: " + $x.prop("color")); }); }); </script> <style> div{ width: 250px; padding: 10px; height: 100px; border: 2px solid green; } </style> </head> <body> <div> <p></p> <br><br> <!-- click on button this method --> <button>Click Here!</button> </div> </body> </html> |
Output:
Before clicking on the “Click Here” button-

After clicking on the “Click Here” button-

Recommended Posts:
- jQuery | last() with Examples
- jQuery | one() with Examples
- jQuery | on() with Examples
- jQuery | after() with Examples
- jQuery | has() with Examples
- jQuery | val() with Examples
- jQuery | eq() with Examples
- jQuery | first() with Examples
- jQuery | focus() with Examples
- jQuery | hasClass() with Examples
- jQuery | insertBefore() with Examples
- jQuery | insertAfter() with Examples
- jQuery | index() with examples
- jQuery | stop() with Examples
- jQuery | queue() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




