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

Igor Kałużny
Igor Kałużny
4,337 Points

$image.attr

Why do we have to do this:

 var imageLocation = $(this).attr("href");
  $image.attr("src", imageLocation);

instead of this:

  $image.attr("src", "href");

I know that it probably won't work, but can smn explain the logic to me please?

2 Answers

Stephan Olsen
Stephan Olsen
6,650 Points

Your example wouldn't work. If you wanted to shorten it down to one line, it would look like this:

  $image.attr("src", $(this).attr("href"));

It's perfectly valid code, but it is much harder to read. It's good practice to break down your code in smaller pieces for more readability. This makes the code much easier to maintain when going back and looking at old code.

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,372 Points

Pointing your solution

$image.attr("src", "href");

This line of code would set the src attribute at the value "href", not what you want. Stephan Olsen gave you a good method if you want to shorten your code if that's what you want.