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

JavaScript jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 3

What if I wanted the actual Name of the attribute?

With the following code:

var imageLink = $(this).attr("href");

I know the .attr("href") will return the value of this attribute.. but what if I actually wanted the "href" the attribute name?

2 Answers

Steven Parker
Steven Parker
231,269 Points

You already know the name is "href" = you need to have the name already to identify the attribute.

Or do you want to get a list of ALL attributes the element has? If so, that would be:

var attribute_list = this.attributes;  // no JQuery needed

attribute_list would then be a NamedNodeMap, each item in the list would have a .name and a .value property, among other things.

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

If you need to get link from image tag, you need to get attribute src