12

I need to show/hide image in html page. I thought its very simple. But why I am getting error 'visible' undefined.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Ajax Test
    </title>
    <script type="text/javascript">
<!--
    function showImage(){
        document.getElementById('loadingImage').style.visibility=visible;
    }

    -->

    </script>
    </head>
<body>
    <input type="button" value="Ajax Button" onclick="showImage();"/>
    <img id="loadingImage" src="ajax-loader.gif" style="visibility:hidden"/>

</body>
2
  • Try typing it in quotes ...style.visibility = "visible" Commented Jan 4, 2010 at 11:54
  • Don't comment out your script: dorward.me.uk/www/comments-cdata Commented Jan 4, 2010 at 11:55

6 Answers 6

16

You need to put it in quotes - it's a string:

document.getElementById('loadingImage').style.visibility='visible';
Sign up to request clarification or add additional context in comments.

Comments

3

I would use Jquery. Go download it at the Jquery home page.

Then, include it:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function showImage(){
$("#loadingImage").toggle();
}

</script>


<img id="loadingImage" src="ajax-loader.gif" style="display:none;"/>

Comments

3

If the other answers don't give you the results you're after, try setting the display to none:

document.getElementById('loadingImage').style.display='none';

Comments

1

I am very very sorry. It should be

  document.getElementById('loadingImage').style.visibility='visible';

quuotes missing aroung visible.

Comments

0

You need to enclose it in quote marks, otherwise JavaScript thinks you're trying to set it to be the value of a variable called "visible". Since you don't have a variable called "visible", you get the error saying that it's undefined.

document.getElementById('loadingImage').style.visibility='visible';

Comments

0

None of this worked for me. All you have to do is set the image classname to a css property with opacity.

JavaScript:

document.getElementById('loadingImage').className = "opacityofimage";

CSS:

.opacityofimage {
    opacity:0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.