How to Display Images in JavaScript ? Last Updated : 29 Dec, 2023 Comments Improve Suggest changes 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 Display 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 How to import a SVG file in JavaScript ? SVG (Scalable Vector Graphics) files in JavaScript are XML-based vector image files commonly used for describing two-dimensional vector graphics. In this article, we are going to see and use different ways of using SVGs. Below are the methods to import an SVG file in JavaScript: Table of Content Usi 3 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 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 Insert an Image in HTML? To insert an image in HTML, you can use <img> tag. This tag uses the src attribute to define the URL of the image file. We can also use CSS to insert image in HTML.Table of ContentUsing img TagUsing background-image propertyInsert an Image using img TagThe <img> tag is the primary method 1 min read Like