How to Show Images on Click using HTML ? - GeeksforGeeks (2024)

Last Updated : 05 Sep, 2024

Summarize

Comments

Improve

Display Images on Click Using HTML and JavaScript refers to techniques that dynamically show images when a user interacts with a webpage, such as clicking a button. This enhances user engagement by making web pages more interactive, using simple HTML, CSS, and JavaScript methods.

Table of Content

  • Changing Display Properties of <img> Attribute using JavaScript
  • Using <img> Tag with Empty src Attribute
  • Using Bootstrap Modal
  • Using JavaScript to Append Image to DOM

Changing Display Properties of <img> Attribute using JavaScript

This approach initially hides the image by setting the <img> tag’s display property to none. When a user clicks a button, a JavaScript function changes the display property to block, making the image visible on the webpage.

Example: In this example, the image is initially hidden using display: none. Clicking the “Show Image” button triggers the show() function, which displays the image and hides the button 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> <!-- Add id to image --> <img id="image" src="https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3.png" 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:

How to Show Images on Click using HTML ? - GeeksforGeeks (1)

Using <img> Tag with Empty src Attribute

This method uses an <img> tag with an empty src attribute, making the image invisible initially. When the user clicks a button, a JavaScript function sets the src attribute to the image URL, displaying the image on the webpage.

Example: In this example, the <img> tag initially has an empty src. Clicking the button triggers the show() function, dynamically setting the src to display the image and hiding the button.

HTML
<!DOCTYPE html><html><head> <title> Using <img> Tag with Empty src Attribute </title></head><body> <div> <h1>GeeksforGeeks</h1> <h3>Click on the button to see image</h3> <!-- img element without src attribute --> <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 ="https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3.png" document.getElementById("btnID") .style.display = "none"; } </script></body></html>

Output:

How to Show Images on Click using HTML ? - GeeksforGeeks (2)

Using Bootstrap Modal

This approach uses a Bootstrap modal to display images. By integrating Bootstrap via CDN, a modal pop-up is triggered when the user clicks a button, showing the image within the modal’s content area, providing a neat and interactive display option.

Example: In this example, a Bootstrap modal is triggered when clicking the “Show image” button. The modal contains an image inside the body and a “Close” button in the footer. Bootstrap and jQuery are used for functionality.

HTML
<!DOCTYPE html><html><head> <title> Using Bootstrap Modal </title></head><body> <h2>GeeksforGeeks</h2> <p><b>Click on the button to see image</b></p> <!-- Button to launch a modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Show image </button> <!-- Modal --> <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"> <!-- Add image inside the body of modal --> <div class="modal-body"> <img id="image" src="https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3.png" 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> <!-- Adding scripts to use bootstrap --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"> </script></body></html>

Output:

How to Show Images on Click using HTML ? - GeeksforGeeks (3)

Using JavaScript to Append Image to DOM

This approach creates a new <img> element dynamically using JavaScript. The image’s src attribute is set to the desired URL, and the element is appended to the DOM, making the image appear on the webpage when a button is clicked.

Example: In this example, clicking the “Show Image” button creates an image container with a close button. The container displays an image and can be closed by removing it from the document.

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 on Click</title> <style> body { text-align: center; } .image-container { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; border: 1px solid #ccc; padding: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); z-index: 9999; max-width: 80%; max-height: 80%; overflow: auto; } /* Styling for the close button */ .close-btn { position: absolute; top: 5px; right: 5px; background-color: #f44336; color: white; border: none; padding: 5px 10px; cursor: pointer; } /* Styling for the image */ .image-container img { max-width: 100%; height: auto; } </style></head><body> <button onclick="showImage()"> Show Image </button> <script> function showImage() { let imageContainer = document.createElement("div"); imageContainer.className = "image-container"; let image = document.createElement("img"); image.src ="https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3.png"; let closeButton = document.createElement( "button" ); closeButton.textContent = "Close"; closeButton.className = "close-btn"; closeButton.onclick = function () { document.body.removeChild( imageContainer ); }; imageContainer.appendChild( closeButton ); imageContainer.appendChild(image); document.body.appendChild( imageContainer ); } </script></body></html>

Output:

How to Show Images on Click using HTML ? - GeeksforGeeks (4)



shubhamvora05

How to Show Images on Click using HTML ? - GeeksforGeeks (6)

Improve

Next Article

Please Login to comment...

How to Show Images on Click using HTML ? - GeeksforGeeks (2024)
Top Articles
Caribbean Islands Closest To The US | BEACHES
Maintaining a portfolio
Spectrum Gdvr-2007
Patreon, reimagined — a better future for creators and fans
Inducement Small Bribe
Access-A-Ride – ACCESS NYC
How To Do A Springboard Attack In Wwe 2K22
Identifont Upload
Comforting Nectar Bee Swarm
Lost Ark Thar Rapport Unlock
Zitobox 5000 Free Coins 2023
Nikki Catsouras Head Cut In Half
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Violent Night Showtimes Near Amc Fashion Valley 18
Mndot Road Closures
Jesus Revolution Showtimes Near Chisholm Trail 8
Clairememory Scam
Select Truck Greensboro
Mlb Ballpark Pal
Miss America Voy Forum
Uky Linkblue Login
Divina Rapsing
Whitefish Bay Calendar
Craigslist Southern Oregon Coast
Vegito Clothes Xenoverse 2
Puretalkusa.com/Amac
Xfinity Outage Map Fredericksburg Va
Hampton University Ministers Conference Registration
Aliciabibs
Water Temperature Robert Moses
Nottingham Forest News Now
HP PARTSURFER - spare part search portal
Shauna's Art Studio Laurel Mississippi
Ucm Black Board
Quality Tire Denver City Texas
In Branch Chase Atm Near Me
Watchdocumentaries Gun Mayhem 2
Viewfinder Mangabuddy
Pensacola Cars Craigslist
Htb Forums
Emily Tosta Butt
Www.craigslist.com Waco
If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
Emily Browning Fansite
Disassemble Malm Bed Frame
Wilson Tire And Auto Service Gambrills Photos
Canvas Elms Umd
Meet Robert Oppenheimer, the destroyer of worlds
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Online TikTok Voice Generator | Accurate & Realistic
Rocket Bot Royale Unblocked Games 66
786 Area Code -Get a Local Phone Number For Miami, Florida
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5838

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.