Skip to content
Related Articles

Related Articles

How to change the background image using jQuery ?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Aug, 2021
View Discussion
Improve Article
Save Article

To change the background image using jQuery, you can use the jQuery CSS() method. For this, the whole property value is specified using the url() functional notation.

Approach: Suppose we have an image URL stored in a variable and then use css() method to change the value of background image.

Below example illustrates the above approach:


<!DOCTYPE html>
<html lang="en">
    jQuery Changing background-image CSS Property
        .box {
            width: 700px;
            height: 220px;
            border: 2px solid black;
            background-repeat: no-repeat;
            background-image: url(
    <script src=""></script>
        $(document).ready(function() {
            // Change background image of a div by clicking on the button
            $("button").click(function() {
                var imageUrl = 
                $(".box").css("background-image", "url(" + imageUrl + ")");
    jQuery Changing background-image CSS Property
    <div class="box"></div>
        <button type="button">
            Change Background Image


  • Before Clicking on the button:
  • After Clicking on the button:
  • jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
    You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.

    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :

    Start Your Coding Journey Now!