user3622187
user3622187

Reputation: 1

Changing background image using the url of a clicked image with jQuery

I have a lists of images with the same class. When I click one of the images, I would like jQuery to grab the source of the image and apply it to the body background. Is that possible?

Upvotes: 0

Views: 34

Answers (2)

Mister Epic
Mister Epic

Reputation: 16733

You are looking for something like this:

$(document).on('click', 'img.myClass', function(){
    var src = $(this).attr('src');
    $(body).css('background-image', src);
});

Upvotes: 1

kravits88
kravits88

Reputation: 13049

$("img.myClass").click(function(image){
    $(body).css('background-image', $(image).attr('src'));
});

Upvotes: 0

Related Questions