How to Show Images on Click using HTML ?
In this article, we are going to learn how can we Show Images on Click using HTML. We will also use some JavaScript as it will help us to create dynamic changes on web applications.
These are the following approaches by using these we can Show Images on Click using HTML:
Changing display properties of <img> attribute using JavaScript:
- In this method, we will set the display property inside the <img> tag using the style attribute.
- We have to set “display: none” for the image. After that, when the user clicks on the button.
- We will call a JavaScript function that will access the image to change its display property to block.
Example: In this example, we will change the display property of <img> attributes using Javascript.
HTML
<!DOCTYPE html>
< html >
< head >
< style >
/* Set display to none for image*/
#image {
display: none;
}
</ style >
</ head >
< body >
< div >
< h1 >GeeksforGeeks</ h1 >
< h3 >Click on the button to see image</ h3 >
< img id = "image"
src =
alt = "GFG image" />
</ div >
< button type = "button" onclick = "show()" id = "btnID" >
Show Image
</ button >
< script >
function show() {
/* Access image by id and change
the display property to block*/
document.getElementById('image')
.style.display = "block";
document.getElementById('btnID')
.style.display = "none";
}
</ script >
</ body >
</ html >
|
Output:
Using <img> tag with empty src attribute
- Obviously, with an empty src value, the user will not be able to see the image on the web page.
- We will set a value of the src attribute using the JavaScript function which is implemented for the user to click on the button.
- However, some browsers such as Chrome do not remove the broken image icon while using this method.
Example: In this example, we will use the <img> tag with an empty src attribute
HTML
<!DOCTYPE html>
< html >
< body >
< div >
< h1 >GeeksforGeeks</ h1 >
< h3 >Click on the button to see image</ h3 >
< img id = "image" src = "" />
</ div >
< button type = "button" onclick = "show()" id = "btnID" >
Show Image
</ button >
< script >
function show() {
/* Get image and change value
of src attribute */
let image = document.getElementById("image");
image.src =
document.getElementById("btnID")
.style.display = "none";
}
</ script >
</ body >
</ html >
|
Output:
Using Bootstrap Modal
- We will use a Bootstrap modal to show an image while clicking on the button.
- We need to integrate the bootstrap CDN with the HTML code to use it.
- We can add bootstrap CDN to our HTML file as added in the below example code.
Example: In this example, we will see the use of the Bootstrap model.
HTML
<!DOCTYPE html>
< html >
< body >
< h2 >GeeksforGeeks</ h2 >
< p >< b >Click on the button to see image</ b ></ p >
< button type = "button"
class = "btn btn-primary"
data-toggle = "modal"
data-target = "#exampleModal" >
Show image
</ button >
< div class = "modal fade"
id = "exampleModal"
tabindex = "-1"
role = "dialog"
aria-labelledby = "exampleModalLabel"
aria-hidden = "true" >
< div class = "modal-dialog" role = "document" >
< div class = "modal-content" >
< div class = "modal-body" >
< img id = "image"
src =
alt = "Click on button" />
</ div >
< div class = "modal-footer" >
< button type = "button"
class = "btn btn-secondary"
data-dismiss = "modal" >
Close
</ button >
</ div >
</ div >
</ div >
</ div >
< script src =
</ script >
< script src =
</ script >
</ body >
</ html >
|
Output:
Last Updated :
24 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...