If I want to change the href of a link dynamically, should I do so using prop()
or attr()
?
prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. So use prop!
The prop() method sets or returns properties and values of the selected elements. When this method is used to return the property value, it returns the value of the FIRST matched element.
jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.
You would use .attr()
, as .prop()
is more commonly used for boolean properties such as checked
, selected
, etc - though it is certainly possible with .prop
it's arguably less clear as per your intent
Though I do believe that ultimately they are very similar (or used to be) functionality-wise
Just a note: the jQuery API site seems to follow the boolean 'sway':
.prop()
- Examples use checked
and disabled
.attr()
- Examples use alt
title
and src
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With