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
Try Retail Therapy to Reduce Stress and Lower Blood Pressure | Hello Heart Blog
14 Common Interview Questions And How To Answer Them - FlexMyFinances.com
Richard Sambade Obituary
Chastity Brainwash
Slushy Beer Strain
Bestellung Ahrefs
Https E24 Ultipro Com
United Dual Complete Providers
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Operation Cleanup Schedule Fresno Ca
Pac Man Deviantart
What Happened To Anna Citron Lansky
Cinebarre Drink Menu
Gdp E124
Does Breckie Hill Have An Only Fans – Repeat Replay
Log in or sign up to view
Sonic Fan Games Hq
Ally Joann
Yard Goats Score
Is A Daytona Faster Than A Scat Pack
Craigslist Maryland Trucks - By Owner
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
Craigslist Pennsylvania Poconos
Hellraiser 3 Parents Guide
§ 855 BGB - Besitzdiener - Gesetze
Umn Biology
Airg Com Chat
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Mgm Virtual Roster Login
About Us | SEIL
Ishow Speed Dick Leak
Cl Bellingham
Conroe Isd Sign In
Top 25 E-Commerce Companies Using FedEx
Dcilottery Login
Fwpd Activity Log
Oppenheimer Showtimes Near B&B Theatres Liberty Cinema 12
Conan Exiles Armor Flexibility Kit
2132815089
Nina Flowers
Jamesbonchai
Wilson Tire And Auto Service Gambrills Photos
6576771660
Mychart University Of Iowa Hospital
Noh Buddy
Toomics - Die unendliche Welt der Comics online
Ts In Baton Rouge
8 4 Study Guide And Intervention Trigonometry
Cryptoquote Solver For Today
Asisn Massage Near Me
Varsity Competition Results 2022
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.