Skip to content Skip to sidebar Skip to footer

How To Set Rails Background Image Url In Javascript?

In my rails application, I have some set of content and image-button (each piece of content has an image). When a user clicks the button, I want to show the respective image. In m

Solution 1:

Try this

$('.view').click(function (event){
    $(this).css("background", "url(assets/images/sample.png) no-repeat");
    });

EDIT

In your case if you wish to set the background according to id of the element, simply modify the code like this:

$('.view').click(function (event){
  var id = $(this).attr('id');
  $(this).css("background", "url(assets/images/" + id + ".png) no-repeat");
});

Post a Comment for "How To Set Rails Background Image Url In Javascript?"