Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ben Os
20,008 Pointsattr() method in JQ
Some might say that the attr() method in JQ "Get or set" the value of a particular attribute for the matched set of elements.
Why is it either get or set? I know that attr() [re]sets values of attributes we pick for elements or selectors.
So I guess, we could say that it "sets" and it's very much sementical and philosophical.
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Ben,
Some jQuery methods like attr() are both getters and setters. Which one it is depends on what arguments are passed in.
Here's the docs page for the attr() method: http://api.jquery.com/attr/
You can see that there are different variations of the attr() method depending on the arguments.
.attr(attributeName) is the getter.
Example:
.attr('href')
This will get the href attribute of the first element of the set of matched elements.
On the other hand, .attr(attributeName, value) is a setter because it will set the value of an attribute.
Example:
.attr('href', 'http://www.example.com');
This will set the href attribute to the value specified for the set of matched elements.