user961694
user961694

Reputation:

jQuery change images on links hover

I have a problem with a code below. Somehow I'm not able to change image background using jQuery script.

HTML code looks like:

jQuery code looks like:

$(document).ready(function(){
        $('.hoverlink').hover(function(){$('.group-image').css('background', $(this).attr('data-img'))});
});

data-image tag contains correct URL to an image for related links, but when I hover any of those links it doesn't add background CSS to group-image div. JSFillde of an examle

Any clue where I have made an error?

Thanks

Upvotes: 1

Views: 486

Answers (2)

JRulle
JRulle

Reputation: 7568

You are missing url('') from your background assignment: DEMO

$('.hoverlink').hover(function(){
    $('.group-image').css('background', "url('" + $(this).attr('data-img') + "')")
});

Upvotes: 0

Raja Sekar
Raja Sekar

Reputation: 2130

$('.hoverlink').hover(function(){
    $('.group-image').css('background-image', 'url('+$(this).attr('data-img')+')')
});

This works!! Updated fiddle

Upvotes: 1

Related Questions