How to Display Image in Alert/Confirm Box in JavaScript ? Last Updated : 02 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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, create the HTML structure with a button to trigger the modal and the modal itself and style it as your need.Then write JavaScript to handle the opening and closing of the modal when the button is clicked or when the user interacts with the modal elements.And add event listeners to the button and modal elements to control their behavior here use CSS to adjust the appearance of the modal content, such as the image and text inside it.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Alert</title> <style> body { overflow: auto; /* Revert back to default */ } .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.5); } .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 80%; max-width: 600px; text-align: center; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } /* Adjusted image size */ .modal-content img { max-width: 100%; height: auto; } /* Text at the bottom */ .modal-text { margin-top: 20px; font-size: 16px; color: #555; } /* Button style */ #openModalBtn { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } #openModalBtn:hover { background-color: #45a049; } #openModalBtn:focus { outline: none; } img { height: 500px; width: 500px; } </style> </head> <body> <!-- Header --> <h1 style="text-align: center;">Geeksforgeeks Alert</h1> <!-- Content --> <p style="text-align: center;"> Click the button below to open the image alert.</p> <!-- The Button --> <div style="text-align: center;"> <button id="openModalBtn">Open Image</button> </div> <!-- The Modal --> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">×</span> <img src=" https://media.geeksforgeeks.org/wp-content/uploads/20240322101847/Default_An_illustration_depictin-(2)-660.jpg" alt="Image"> <div class="modal-text"> You clicked the img alert button..... </div> </div> </div> <script src="script.js"></script> </body> </html> JavaScript // script.js let modal = document.getElementById("myModal"); let btn = document.getElementById("openModalBtn"); let span = document.querySelector(".close"); btn.addEventListener('click', function () { modal.style.display = "block"; }); span.addEventListener('click', function () { modal.style.display = "none"; }); window.addEventListener('click', function (event) { if (event.target == modal) { modal.style.display = "none"; } }); Output: Image in alert/confirm box in JavaScript Comment More infoAdvertise with us Next Article How to Display Image in Alert/Confirm Box in JavaScript ? skaftafh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Display Images in JavaScript ? 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, 2 min read How to Display JavaScript Variable Value in Alert Box ? An alert box is a dialog box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificatio 2 min read How to Display JavaScript Variable Value in Alert Box ? JavaScript is a powerful scripting language widely used in web development to enhance user interactivity and functionality. Often, developers need to display variable values to users, either for debugging purposes or to provide informative messages. One common method to achieve this is by using an a 2 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 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 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 Create Customizable Alerts in JavaScript ? Alerts in JavaScript are an important components in web design. They are commonly used to notify users. The SweetAlert2 library aims to make basic looking alerts much more attractive and also provide context to the alert. The documentation and usage examples of SweetAlert2 could be found here. Insta 2 min read How to Change the Color of the Alert Box in JavaScript ? Alert boxes, often utilized in JavaScript applications, provide users with important notifications or messages. However, their default appearance may not always blend seamlessly with the overall design of your website or application. JavaScript offers several methods to customize alert boxes as list 3 min read How to Change the Color of the Alert Box in JavaScript ? An alert box is a dialogue box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificat 5 min read Like