0

What could be the problem? The console shows me that the variable y contains the right absolute path of an existing external image (direct linking enabled). After the background setting font color turns to red.

console.log("image url: "+y);
       $(this).css("background","url('"+y+"') !important;");
       $(this).css("color","red");

It does not work with background-image either.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rápli András
  • 3,869
  • 1
  • 35
  • 55

5 Answers5

4

Remove the !important;.

Read this if you have to use that declaration: How to apply !important using .css()?

Community
  • 1
  • 1
gvee
  • 16,732
  • 35
  • 50
0

Have you tried this?

$(this).css({
     backgroundImage:"url('"+y+"') !important"
     });

I recommend to use an object when you pass multiple css styles to the same element rather using the .css command multiple times:

$(this).css({
         backgroundImage:"url('"+y+"') !important",
         color: "red"
         });
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
0

Try

$(this).css("background","url('" + y + "') !important");
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

I would put it in a single call to css like:

$(this).css({"background":"url('"+y+"') !important", "color":"red"});
David MacCrimmon
  • 966
  • 1
  • 6
  • 19
0

As others have stated, it's better to pass an object rather than calling the css function several times. Also, in my opinion, it's more clear if you don't use css shorthands.

Anyways, try it with double quotes in the url.

$(this).css({'background-image':'url("'+y+'") !important', 'color':'red'});
Nefreo
  • 2,162
  • 1
  • 15
  • 24