The prop method
The prop method allows you to get and change the property of an element. It is important not to confuse properties and attributes of elements. When creating a DOM tree, attributes are translated into the corresponding DOM properties of elements. Their names do not always match, as do their values. Some properties may also not have corresponding attributes, they can only be obtained and set using the prop method, for example the tagName property. To get the attributes of an element, use the attr method.
Syntax
Getting the value of a property:
$(selector).prop(property name);
Changing the value of a property:
$(selector).prop(property name, new value);
Changing the values of multiple properties:
$(selector).prop({first_property: new_meaning, second_property: new_meaning, ...});
Applying a function to each element in a set:
$(selector).prop(property name, function(number in set, current property value));
Example
In the following example, we get a paragraph by its id and display the value of its className property:
<p id="test" class="aaa">text</p>
let value = $('#test').prop('className');
console.log(value);
Example
Let's change the properties for className and disabled at the same time:
<input type="checkbox" class="aaa" id="test" />
$("#test").prop({ className: 'bbb', disabled: 'true' });
See also
-
method
removeProp,
which allows you to delete properties of elements -
method
attr,
which allows you to work with element attributes