Solved

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

Posted on 2008-06-19
8
662 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:ohmErnie
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 21823324
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>
0
 
LVL 1

Author Comment

by:ohmErnie
ID: 21823378
Well, when I look at the source code, I don't even see my image. Is that because I set it invisible in ASP?
0
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 21823422
if you set it to visible=false in the code behind it is never output in the html,
if you add the style attribute  img.Style.Add("visibilty", "hidden");
it will be output to the page so you can access it
0
 
LVL 14

Expert Comment

by:ali_kayahan
ID: 21823428
<script type="text/javascript">
        function show_wait()
        {
            var img;
            img = document.getElementById("img_load");
            img.style.display = 'block';  
        }
    </script>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:burakiewicz
ID: 21823433
should be
("visibility", "hidden");
0
 
LVL 33

Expert Comment

by:raterus
ID: 21823442
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
0
 
LVL 7

Expert Comment

by:ruscomp
ID: 21823452
It may also not be shown if you are using update panels... AJAX loaded html won't show in the source...
0
 
LVL 1

Author Closing Comment

by:ohmErnie
ID: 31468838
That worked great. All I had to do was change my javascript to set the visibility back to "visible".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 57
Server Error 11 48
How can I get this SlideToggle to behave? 6 19
What namespace do I need to import? 2 14
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now