Mansoor Akhtar
Mansoor Akhtar

Reputation: 2316

Changing Background Image through JQuery

i want to change background image by using jquery. below is given html element i want to change its background image.

<div class="logo" />

and this is jquery code for changing background image.

$(".logo").css({ 'background-image' : 'url(test(1).png)' }

the problem is that when image name containing parenthesis () then its not working , but without parenthesis its working.. is there any solution to change background image regardless of its name.

Upvotes: 0

Views: 90

Answers (2)

black_pottery_beauty
black_pottery_beauty

Reputation: 879

$(".logo").css({ 'background-image' : 'url("' + imageUrl + '")' });

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337714

Put the value in double quotes:

$(".logo").css({ 'background-image' : 'url("test(1).png")' });

Also note that you don't need to use an object here as you're setting only one CSS rule:

$(".logo").css('background-image', 'url("test(1).png")');

Upvotes: 5

Related Questions