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

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,373 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.