How to Create an Image Element using JavaScript? Last Updated : 24 May, 2025 Comments Improve Suggest changes Like Article Like Report 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 conditions.Approach to Create an Image Element in JavaScriptCreate the Image Element: Use document.createElement('img') to create an empty <img> tag.Set the Image Source (src): Assign the image URL to the src attribute.Set the Alternative Text (alt): Set a description of the image for accessibility using the alt attribute.Set the Title Attribute: Add a title attribute for a tooltip when the user hovers over the image.Append the Image to the Document: Insert the image element into the document by appending it to a container (like body or any other element).Now let's understand this with the help of example: html <html> <head></head> <body id="body" style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Click on the button to add image element </h3> <button onclick="GFG_Fun()"> click here </button> <h2 id="result" style="color:green;"></h2> <script> let res = document.getElementById('result'); function GFG_Fun() { let img = document.createElement('img'); img.src = 'https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png'; document.getElementById('body').appendChild(img); res.innerHTML = "Image Element Added."; } </script> </body> </html> Output:In this exampleA button is created to trigger a function when clicked.The function creates an image element.Sets its src to a specific URL.Adds the image to the page and displays a message: "Image Element Added.JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.ConclusionCreating an image element dynamically using JavaScript offers flexibility by allowing images to be added to a webpage without manually writing the <img> tag. This technique is particularly useful when images need to be added or manipulated based on user actions or other conditions. Comment More infoAdvertise with us Next Article How to Create an Image Element using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Create Image Gallery using JavaScript? An Image Gallery is a collection of images displayed in a grid or slideshow format on a webpage. To create an Image Gallery using JavaScript, you can dynamically load images, create HTML elements, and use CSS for styling. JavaScript can add interactivity, like transitions and navigation controls.Her 3 min read How to create an HTML element using jQuery ? In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. The jQuery append() method is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) 2 min read How to create an element from a string in JavaScript ? In this article, we will learn how to create an element from a string using JavaScript. This can be used in situations where dynamically generated elements are required by the user. This can be achieved using many approaches as given below: Table of Content Using the createElement() methodUsing the 3 min read How to create an image map in JavaScript ? An image map is nothing but an image that is broken into various hotspots and each hotspot will take you to a different file. Hotspots are nothing but clickable areas which we create on an image by using the <area> tag. This type of map is called a client-side image map as the map is embedded 3 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 Creating a Simple Image Editor using JavaScript In this article, we will be creating a Simple Image Editor that can be used to adjust the image values like brightness, contrast, hue, saturation, grayscale, and sepia. Image editors allow one to quickly edit pictures after they have been captured for enhancing them or completely changing their look 10 min read How to show Image in an Alert Box using JavaScript ? Adding images in JavaScript alerts can make messages more eye-catching and easier to understand. It's a way to attract your attention and make the alerts more attractive. We will show the steps on how to put images in our JavaScript alerts and make your notifications clearer and more engaging for us 3 min read How to Crop an Image in JavaScript? To crop an image in JavaScript, you can use the <canvas> element to manipulate the image's dimensions and crop it to a specified area. This approach is flexible and works entirely in the browser without needing external libraries. Here's a step-by-step example demonstrating how to crop an imag 3 min read How to get the Width and Height of an Image using JavaScript? Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then 2 min read How to get the image size (height & width) using JavaScript? To get the size of an image (height and width), Element.clientHeight and Element.clientWidth properties are used. Element.clientHeight: We can access the inner height of an element with this property. This height includes the padding but not the margin and border. Element.clientWidth: We can access 2 min read Like