Link to home
Start Free TrialLog in
Avatar of ohmErnie
ohmErnie

asked on

javascript error when trying to use getElementById: 'null' is null or not an object?

I thought this was pretty simple. I have an ASP:Image named img_load. I have a javascript set up to run onclick. All I want to do is set the visibility to true. Simple right? But I'm getting a 'null' is null or not an object error when trying to do anything with my javascript object.  The image is in an update panel. Does that make a difference? Is that what's causing my error?
<script type="text/javascript">
        function show_wait()
        {
            var img;
            img = document.getElementById("img_load");
            img.visible = true;   
        }
    </script>

Open in new window

Avatar of raterus
raterus
Flag of United States of America image

Look at the source of the page as the browser sees it, find the image and use the ID it has.  My guess is it's been rewritten by asp.net.

To get this "renamed" id on the server, you need to use the ClientID function of the control.  I've used this in the past to easily make the switch.

<script type="text/javascript">
        function show_wait()
        {
            var img;
            img = document.getElementById('<%= img_load.ClientID %>');
            img.visible = true;  
        }
    </script>
Avatar of ohmErnie
ohmErnie

ASKER

Well, when I look at the source code, I don't even see my image. Is that because I set it invisible in ASP?
ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<script type="text/javascript">
        function show_wait()
        {
            var img;
            img = document.getElementById("img_load");
            img.style.display = 'block';  
        }
    </script>
should be
("visibility", "hidden");
Yeah, if you set Visible=false on the server control, it won't even be rendered.  What you need to do is set CSS to hide the image, but the HTML will remain.

The CSS attribute to use is display:none or display:block
It may also not be shown if you are using update panels... AJAX loaded html won't show in the source...
That worked great. All I had to do was change my javascript to set the visibility back to "visible".