Home >>jQuery Tutorial >jQuery prop() Method

jQuery prop() Method

jQuery prop() Method

jQuery prop() method in jQuery is used to sets or returns values and properties of the selected elements, it returns the value of the FIRST matched element.

Syntax:
$(selector).prop(property,value)
Return the value of a property: $(selector).prop(property)
Set the property and value: $(selector).prop(property,value)
Set property and value using a function: $(selector).prop(property,function(index,currentvalue))
Set multiple properties and values: $(selector).prop({property:value, property:value,...})

Parameter Values

Parameter Description
property It is used to specifies the name of the property
value It is used to specifies the value of the property
function(index,currentvalue) It is used to specifies a function which returns the property value to set
  • index – it is used to receives the index position of the element
  • currentvalue – it is used to receives the current property value of selected elements
Here is an Example of jQuery prop() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
var $x = $(".prop");
$x.prop("color", "c1c1c1c1");
$x.append("The color property value: " + $x.prop("color"));
$x.removeProp("color");
$x.append("<br>color property value: " + $x.prop("color"));
});
});
</script>
</head>
<body>
<button class="btn1">Click me to Add and remove</button><br><br>
<div class="prop"></div>
</body>
</html>

Output: