How to Display Images in JavaScript ? Last Updated : 29 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report To display images in JavaScript, we have different approaches. In this article, we are going to learn how to display images in JavaScript. Below are the approaches to display images in JavaScript: Table of Content Using CreateElementUsing InnerHTMLApproach 1: Using CreateElementIn an HTML document, the document.createElement() is a method used to create the HTML element. The element specified using elementName is created or an unknown HTML element is created if the specified elementName is not recognized. Syntax:let element = document.createElement("elementName");Example: In this example, we will display an image using CreateElement. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Display Image</title> </head> <body> <script> function show_image(src, width, height,alt) { // Create a new image element let img = document.createElement("img"); // Set the source, width, // height, and alt attributes img.src = src; img.width = width; img.height = height; img.alt = alt; // Append the image element // to the body of the document document.body.appendChild(img); } // Example usage: show_image( "https://media.geeksforgeeks.org/wp-content/uploads/20231228172727/gfg-image.jpg", 300, 200,"gfg logo"); </script> </body> </html> Output: Approach 2: Using InnerHTMLThe DOM innerHTML property is used to set or return the HTML content of an element. Syntax:It returns the innerHTML Property. Object.innerHTMLExample: In this example, we are using InnerHTML. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Display Image</title> </head> <body> <!-- Create a container element where the image will be displayed --> <div id="imageContainer"></div> <script> // You can dynamically set the innerHTML // of a container to include an image element const imageContainer = document .getElementById("imageContainer"); imageContainer.innerHTML = `<img src= "https://media.geeksforgeeks.org/wp-content/uploads/20231228172727/gfg-image.jpg">`; </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to use document.images in JavaScript ? A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to display images from an array in JavaScript ? Displaying images from an array in JavaScript involves storing image URLs within the array and dynamically generating HTML elements, such as <img>, using JavaScript. By iterating over the array, each image URL is used to create <img> elements, which are then appended to the document for 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 How to Display Image in Alert/Confirm Box in JavaScript ? This article explores how to display an image in an alert box to enhance the user experience with a visually appealing interface. The standard JavaScript alert() method doesn't support images. Table of Content By creating a custom alert box in JavaScriptUsing SweetAlert LibraryBy creating a custom a 3 min read How to Display Image in Alert/Confirm Box in JavaScript ? Using images in alert or confirm boxes in JavaScript adds visual effect and shows the clarity of alert type. With the help of images in alert, messages become more memorable, and accessible, and can attract the user's attention, and the overall user experience is good for use. ApproachAt first, crea 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 display images in Angular2 ? We can serve an image in angular2 by first placing the image in the assets directory of your project where you can create a specific directory for the images or just keep it in the assets directory as it is. Once you have put all the required images in the assets directory open the specific componen 2 min read Like