JQuery how to change background image

JQuery how to change background image

In last post we saw how to add background image inside DIV tag. In this tutorial we will change background image of Div dynamically using JQuery.

JQuery Script to change background image dynamically

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript"> 
   var imagesArray = ["url('http://i2.ytimg.com/vi/mIs4sXi3INA/hqdefault.jpg')", 
     var currentIndex = 0;
     var imagesCount = imagesArray.length;
         if(currentIndex == imagesCount )
         currentIndex = 0;


In above code you can see we have stored Url of images inside an array called imagesArray. You can also get those array dynamically via JQUERY AJAX API or JSON request.

Syntax for changing background image


To access clicked instance which has raised the event, you can use $(this) inside the function.

Once you get that element, attach update required property like css or html of it.



JQuery Change Div background Color and Text Color On Clickasp net image uploadAsp.net Convert String To ImageJquery image click eventAsp.net Convert Image To Base64How to check image is loaded using JQueryDiv Background ImageLearning JqueryJQuery Select ChangeJquery Gridview Dropdownlist Selected Change Enable Disable ControlsJQuery Slide


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.