Skip to main content

How to display an image in Javascript onclick

You can display an image in javascript using onclick event. You have to pass onclick event to button and call the function to display image

  • Display image on click button using javascript arrow function

    <button onClick="showImage()">Button</button>
    <div id="first" style="height:400px; width:400px; display:none;">
        <img src="http://w3codegenerator.com/img/sample-image/new_zealand-300x225.jpg"/> 
    </div>
      
    <script>
        const showImage = () => {
            document.getElementById("first").style.display ='block';
        }
    </script>

    This code snippet will help you to display an image on click button using arraw function with onclick event.

  • Back to code snippet queries related javascript