Using jQuery to change the background image

I am trying to use jQuery to change the background image in a table cell, the table cell has its own class .active. I use jQuery to change other elements in the same place and they all work fine, so I think there should be something wrong in the syntax. the function I use is executed after the button is clicked. my code is:

function vehicle(arg){
  $(".active").css("color", "blue");  
  $(".active").css("background-image", "url(../img/car.png)");
};

CSS

.active{
  background-size: 10px 10px;
  background-repeat: no-repeat;
  border-right: 1px solid none;

The first line runs fine, I tried the following code plus resized the image in every way that I can think of:

   $(".active").css("background-image", "../img/car.png");
   $(".active").css("background-image", "url('../img/car.png')");

can anyone point out what i did wrong?

+4
source share
3 answers

.css() . :

$(".active").css("background-image", "url('img/car.png')");

, img/ , , . Else .

+5

, .

$( ". active" ). attr ( "", "background-image: url (' url')" );

+2

You can set the CSS class to change the image.

CSS

.img1{
    background-image:img/car1.png;
}

.img2{
    background-image:img/car2.png;
}

Jquery:

$(".active").removeClass('img1').addClass('img2');
-1
source

Source: https://habr.com/ru/post/1627761/


All Articles