How to change the background image using jQuery ?
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:
Example:
<!DOCTYPE html> <html lang="en"> <head> <title> jQuery Changing background-image CSS Property </title> <style> .box { width: 700px; height: 220px; border: 2px solid black; background-repeat: no-repeat; background-image: url( } h1{ color:green; } </style> <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 + ")"); }); }); </script> </head> <body> <center> <h1>GeeksforGeeks</h1> <h3> jQuery Changing background-image CSS Property </h3> <div class="box"></div> <p> <button type="button"> Change Background Image </button> </p> </center> </body> </html> |
chevron_right
filter_none
Output:
Recommended Posts:
- JQuery | Setting background-image using CSS property
- HTML | Change Background color using onmouseover property
- How to change the background color after clicking the button in JavaScript ?
- Set the size of background image using CSS ?
- CSS | background-image Property
- How to stretch and scale background image using CSS?
- How to give text or an image transparent background using CSS?
- Background Subtraction in an Image using Concept of Running Average
- How to change color of PNG image using CSS?
- How to change an input button image using CSS?
- How to change text color depending on background color using JavaScript?
- CSS | Animation to Change the Hover State of a Button/Image
- How to change the element id using jQuery ?
- jQuery | change() with Examples
- How to change an HTML element name using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



