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 in CSS

In the video the image location is defined in the HTML but what if I have defined it in CSS. How do I select the url of the image.from CSS using this example from the video?

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

1 Answer

Stephan Olsen
Stephan Olsen
6,650 Points

The jQuery Documentation gives a great example on how to use this.

$( "div" ).click(function() {
// Save the value of the property into a variable
  var color = $( this ).css( "background-color" );
});
Igor Kałużny
Igor Kałużny
4,337 Points

Thank you very much. What if I define the url as:

background: grey url('../img/xx.jpg) cover;

instead of

background-image: url('../img/xx.jpg) cover;

Is it possible to select the url from 'background' property?

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

In fact I didnt even have to change "backround" property to "background-image" but jquery is selecting the url of a clicked image. This seems to work:

$(".picture").click(function(event){

 var imageLocation = $(this).css("background-image");
 $image.css("background-image", imageLocation);
});