Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,451 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