How to Change the src Attribute of an Image Element in JavaScript / jQuery? Last Updated : 15 Oct, 2024 Comments Improve Suggest changes Like Article Like Report To change the src attribute of an image element in JavaScript or jQuery, you can dynamically update the image by setting a new value for the src attribute.Below are the methods to change the src attribute of an image element:Table of ContentUsing JavaScript DOM methodsUsing jQuery prop() MethodUsing JavaScript DOM methodsThis HTML page changes the src attribute of an image when a button is clicked. It starts by displaying an image and a button. A JavaScript function GFG_Fun() changes the image's src upon the button click and updates the text below the image to indicate that the src attribute has been modified.Example: This example changes the src attribute of the image by using JavaScript. html <!DOCTYPE HTML> <html> <head> <title> Change the src attribute of an img tag </title> </head> <body style="text-align:center;"> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <img id="img" src= "https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png" /> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on button to change the src of image"; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { document.getElementById("img").src = 'https://media.geeksforgeeks.org/wp-content/uploads/20190529122826/bs11.png'; down.innerHTML = "src attribute changed"; } </script> </body> </html> Output: Using jQuery prop() MethodThis method set/return properties and values of the matched elements. If this method is used to return the property value, it returns the value of the first selected element. If this method is used to set property values, it sets one or more property/value pairs for the set of selected elements. Example: This example changes the src attribute of image by using JQuery. html <!DOCTYPE HTML> <html> <head> <title> Change the src attribute of an img tag using jQuery </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style="text-align:center;"> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <img id="img" src= "https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png" /> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> $("#GFG_UP").text("Click on button to change the src of image"); $('button').on('click', function (e) { $('#img').prop('src', 'https://media.geeksforgeeks.org/wp-content/uploads/20190529122826/bs11.png'); $("#GFG_DOWN").text("src attribute changed"); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Change the src Attribute of an Image Element in JavaScript / jQuery? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JQuery Similar Reads How to change the text and image by just clicking a button in JavaScript ? The image and text can be changed by using JavaScript functions and then calling the functions by clicking a button. We will do that into 3 sections., in the first section we will create the structure by using only HTML in the second section we will design minimally to make it attractive by using si 2 min read How to Return the Number of Images in a Document with JavaScript? We will learn how we can return the number of images in a document with JavaScript. We need to access and manipulate the DOM (Document Object Model) of the webpage. The DOM represents the structure of an HTML document as a tree of objects. JavaScript can interact with the DOM to manipulate and retri 3 min read How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit 2 min read How to Change Background Image in javaScript? This article will show you how to change the background color in JavaScript. Changing a background image in JavaScript involves modifying the style property of an HTML element to update its backgroundImage property. This can be done by selecting the element and setting its style.backgroundImage prop 2 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read How to change href attribute of a hyperlink using jQuery ? Changing the href attribute of a hyperlink using jQuery refers to the process of modifying the URL a link directs to without altering the HTML manually. This capability is useful for creating dynamic web applications where link destinations need to change based on user interactions or other conditio 2 min read How to replace an HTML element with another one using JavaScript? Replacing an HTML element with another using JavaScript allows for dynamic modification of webpage content without reloading the entire page. Document Object Model (DOM) is a platform and language-neutral interface that is used by programs and scripts to dynamically access the content, style, and st 2 min read How to change the href value of a tag after click on button using JavaScript ? JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML document 4 min read How to use document.images in JavaScript ? In JavaScript, the document.images property returns a collection of all "<img>" elements within the current document. We can manipulate these images using various properties and methods. We will see how to work with document.images in JavaScript. ApproachFirst, create a basic HTML structure an 2 min read How to pass image as a parameter in JavaScript function ? We all are familiar with functions and their parameters and we often use strings, integers, objects, and arrays as a parameter in JavaScript functions but now will see how to pass an image as a parameter in the JavaScript functions. We will use vanilla JavaScript here. Approach: First, create a func 2 min read Like