Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should href be set with prop() or attr()? [duplicate]

Tags:

jquery

attr

prop

If I want to change the href of a link dynamically, should I do so using prop() or attr()?

like image 713
Francisc Avatar asked Aug 12 '13 14:08

Francisc


People also ask

Should I use ATTR or prop?

prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. So use prop!

What is 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.

What is .attr in jQuery?

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.


1 Answers

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

like image 157
MDEV Avatar answered Nov 08 '22 06:11

MDEV