Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to show image in alert box using JavaScript?
JavaScript's default alert() function cannot display images. To show an image in an alert-like dialog, you need to create a custom modal dialog using HTML, CSS, and JavaScript.
Why Default alert() Cannot Show Images
The browser's built-in alert() function only accepts text strings. It cannot render HTML elements like images, making a custom solution necessary.
Creating a Custom Image Alert
Here's a complete example that creates a custom alert dialog with an image:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function showImageAlert(msg) {
var alertBox = $("#imageAlert");
alertBox.find(".message").text(msg);
alertBox.find(".close-btn").click(function() {
alertBox.hide();
});
alertBox.show();
}
</script>
<style>
#imageAlert {
display: none;
background-color: #FFFFFF;
border: 2px solid #333;
position: fixed;
width: 300px;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
padding: 20px;
box-sizing: border-box;
text-align: center;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
border-radius: 8px;
}
#imageAlert button {
background-color: #48E5DA;
border-radius: 5px;
border: 1px solid #aaa;
padding: 8px 16px;
cursor: pointer;
margin-top: 10px;
}
#imageAlert .message {
margin-bottom: 15px;
font-weight: bold;
}
#imageAlert img {
border-radius: 5px;
margin: 10px 0;
}
</style>
</head>
<body>
Alert with Image
<img src="https://via.placeholder.com/150x100/4CAF50/white?text=Success"
width="150" height="100" alt="Alert Image"/>
<br>
<button class="close-btn">OK</button>
<input type="button" value="Show Image Alert" onclick="showImageAlert('This is a custom alert with an image!');" />
</body>
</html>
How It Works
The solution uses these key components:
- Hidden div: Acts as the custom alert container
- CSS positioning: Centers the dialog on screen
- jQuery: Handles showing/hiding and user interaction
- Image element: Displays the desired image within the alert
Alternative Without jQuery
For a pure JavaScript solution without external dependencies:
<!DOCTYPE html>
<html>
<head>
<style>
.custom-alert {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border: 2px solid #333;
padding: 20px;
text-align: center;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
z-index: 1000;
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 999;
}
</style>
</head>
<body>
<p>Custom Alert with Image</p>
<img src="https://via.placeholder.com/120x80/FF5722/white?text=Alert" width="120" height="80">
<br><br>
<button onclick="closeAlert()">Close</button>
<button onclick="showAlert()">Show Custom Alert</button>
<script>
function showAlert() {
document.getElementById('overlay').style.display = 'block';
document.getElementById('customAlert').style.display = 'block';
}
function closeAlert() {
document.getElementById('overlay').style.display = 'none';
document.getElementById('customAlert').style.display = 'none';
}
</script>
</body>
</html>
Key Features
- Modal behavior: Blocks interaction with background content
- Responsive design: Centers properly on different screen sizes
- Customizable: Easy to modify styling and content
- Cross-browser: Works in all modern browsers
Conclusion
Since JavaScript's native alert() cannot display images, custom modal dialogs are the solution. Use HTML/CSS for structure and JavaScript for interaction to create image-enabled alert boxes.
Advertisements
