How to show Image in an Alert Box using JavaScript ? Last Updated : 05 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 users. ApproachFirst, create a basic HTML and provide a CSS style. In the HTML code provide a class or id in every tag which helps you to get a reference in JavaScript.In the javascript get the reference for all the needed tags and then handle the "click" event for the button's using "addEventListener".You can add the description of the alert type at the bottom of time for that first create an element using the "createElement()" method and append it in the model using the "appendChild()" method When you close the alert remove it using the "removeChild()" method.Example: This example shows an image in an alert box using JavaScript HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Show image in alert box using JavaScript </title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="main_container"> <h1>Geeksforgeeks Welcome you to Show image in alert </h1> <p>Here's some good content for you:</p> <ul> <li>OpenAI Python API - Complete Guide</li> <li>OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industr... </li> </ul> <button id="alert_button"> Show Image button </button> </div> <!-- The Modal --> <div id="myModal" class="modal"> <span class="close" id="close_img"> × </span> <div class="modal-content" id="modal-content"> <img id="modal-img"> </div> </div> <script src="script.js"></script> </body> </html> CSS body { font-family: Arial, sans-serif; background-color: #0ee25c; margin: 0; padding: 0; } .main_container { width: 90%; max-width: 800px; margin: 10rem auto; padding: 20px; background-color: #fff; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { color: #333; text-align: center; } p { color: #666; line-height: 1.6; } ul { list-style-type: none; padding: 0; margin: 0; text-align: left; } li { margin-bottom: 10px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); transition: background-color 0.3s; cursor: pointer; } li:hover { background-color: #e0e0e0; } button { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; font-size: 16px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } button:hover { background-color: #fff; color: #0ee25c; border: 2px solid #007bff; } .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.9); } .modal-content { margin: auto; width: 80%; max-width: 700px; background-color: #fff; padding: 20px; text-align: center; border-radius: 10px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .modal-content img { width: 100%; max-width: 100%; height: auto; display: block; margin-bottom: 20px; } .close { color: #ccc; position: absolute; top: 10px; right: 25px; font-size: 30px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #fff; text-decoration: none; cursor: pointer; } @media screen and (max-width: 768px) { .main_container { width: 90%; margin: 5rem auto; } } @media screen and (max-width: 480px) { .main_container { width: 90%; margin: 3rem auto; } } JavaScript let modal = document.getElementById("myModal"); let modalContent = document.getElementById("modal-content"); let modalImg = document.getElementById("modal-img"); let alert_button = document.getElementById("alert_button"); let close_img = document.getElementById("close_img"); let imgSrc = "https://media.geeksforgeeks.org/wp-content/uploads/20230602174859/OpenAI-Python-Tutorial-.webp"; alert_button.addEventListener("click", function () { modal.style.display = "block"; modalImg.src = imgSrc; let image_text = document.createElement("p"); image_text.textContent = "You clicked the image button!"; modalContent.appendChild(image_text); }); close_img.addEventListener("click", function () { let image_text = modalContent.querySelector("p"); modalContent.removeChild(image_text); modal.style.display = "none"; }); Output: Output Comment More infoAdvertise with us Next Article How to show Image in an Alert Box using JavaScript ? skaftafh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Change Button Label in Alert Box using JavaScript ? In JavaScript, the alert method is used to display an alert box with a message. By default, the alert box contains an "OK" button. We cannot directly update the default alert box, However, we can customize the button label by creating a custom alert box using HTML, CSS, and JavaScript. ApproachCreat 2 min read How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange 2 min read How to upload an image using HTML and JavaScript in firebase ? Firebase is a product of Google which helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way. No programming is required on the firebase side which makes it easy to use its features more efficiently. It provides cloud st 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 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 Design a Custom Alert Box using JavaScript ? An alert box is a built-in feature in JavaScript that displays a small window with a message to the user. It's primarily used for providing information to the user, displaying warnings, or prompting the user for confirmation. The below approaches can be used to create a custom alert box in JavaScrip 4 min read How to Customize the Position of an Alert Box using JavaScript ? In web development, alert boxes are frequently used to inform users of important information. However, alert boxes always appear in the center of the screen by default, which might not be the best arrangement for some layouts. This article explores various methods for achieving this personalization. 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 use the alert() method in JavaScript ? In this article, we will learn how to use the alert() method in JavaScript. The alert() method is used to show an alert box on the browser window with some message or warning. We can use it as a message or as a warning for the user. Approach: To show an alert on the browser window, we make a button. 2 min read How to Create an Alert in JavaScript ? The alert() method in JavaScript displays an alert box with a message and an OK button. It's used when you want information to come through to the user, providing immediate notifications or prompts for user interaction during program execution. Note: Alert boxes interrupt user interaction, shifting 1 min read Like