ASA2k ASA2k - 1 year ago 63
HTML Question

How to make an Image display/hide using a button

I'm using the following code to make an image when a button is pressed:

<title>Image Display Test</title>
<script type="text/javascript">
function showImage(){

<input type="button" value="Show Button" onclick="showImage();"/>
<img id="loadingImage" src="pickups1.png" style="visibility:hidden"/>

So far it works, but then the image remains on the screen, Is there anyway to make it that if clicked again the image disappears?

Answer Source

You can check if image is visible and then hide/show it accordingly:

var elem = document.getElementById('loadingImage');
if ( === 'visible') { = 'hidden';
} else { = 'visible';

or using shortcut (ternay operator):

var elem = document.getElementById('loadingImage'); = === 'visible' ? 'hidden' : 'visible';