jQuery prop: Main Tips
- jQuery
.prop()
method returns the property value of the first matched element. .prop()
jQuery can also set properties for elements.- To remove the properties set with this method, use jQuery .removeProp().
Using .prop()
The jQuery .prop()
function gets or sets the property value of the selected element. The function returns undefined
if the value for a property is not assigned.
In the example, clicking a button changes the property value of checkboxes:
$("button").click(() => {
$("input[type='checkbox']").prop({
disabled: false
});
});
Tip: you can get values of each element by using .each() or .map() functions.
Follow this jQuery .prop()
syntax to return the specified property's value:
$("selector").prop(property);
Note: the property is defined by a string.
Now this syntax is used to set a value for one property:
$("selector").prop(property, newValue);
You can also add a function returning the value needed as the newValue
.
To set values for multiple elements using .prop()
jQuery method, put them in pairs:
$("selector").prop(property:value, property:value, ...);
Note: use .prop()) in jQuery to set tagName, nodeType, selectedIndex, nodeType, etc. They cannot be set with .attr() since jQuery 1.6.