Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Show Images on Click using HTML ?

  • Last Updated : 09 Nov, 2021

Occasionally, web pages might contain images that are not relevant to the content of that page. These unusual images are loaded while the web page is loading, and this increases the loading time. More loading time on web pages can decrease the number of visitors.

In this article, we come up with a solution to this problem. We will learn how to show images only when users click on them using HTML, CSS, and JavaScript.

Changing display properties of <img> attribute using JavaScript: In this method, we will set 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.

Steps:

  • Create <img> element in the HTML code.
  • Add style to <img> element and set display properties to none.
  • Create a JavaScript “show()” function that can access the image and change the display property to block.
  • Add button in HTML code which calls “show()” function when user clicks on it.

  
 

Example:

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=
            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 click on the button. However, some browsers such as Chrome does not remove the broken image icon while using this method.

Steps:

  • Create <img> element with empty src attribute.
  • Create “show()” function in JavaScript which can get the image and add the image source link to the src attribute.
  • Add HTML button which calls “show()” function, when user clicks on it.

Example:

HTML




<!DOCTYPE html>
<html>
 
<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 =
 
            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.

Steps:

  • Add bootstrap CDN to HTML file.
  • Create Bootstrap modal and add an image to the body of the modal.
  • Create an HTML button to trigger a modal.

Example:

HTML




<!DOCTYPE html>
<html>
<head>
   
    <link rel="stylesheet" href=
        integrity=
"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
        crossorigin="anonymous">
</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=
                        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 -->
            integrity=
"sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous">
    </script>
    <script src=
            integrity=
"sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous">
    </script>
    <script src=
            integrity=
"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous">
    </script>  
</body>
 
</html>

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!