[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GetElementById find element/exists Javascript?

Posted on 2007-10-05
13
Medium Priority
?
3,793 Views
Last Modified: 2009-12-16
I am coding a .NET page C# and I have a javascript function which I need to check for before I do anything with it.  It is a gridview but it is empty when the page loads.  No matter what I do when checking to see if the grid exists, I get the 'object required' error.  Why?  

if (document.getElementById('foo') == null) {alert('foo is missing!');}
//or:
if ( ! document.getElementById('foo')) {alert('foo is missing!');}

Is there another way to check for this id foo which will work and not return the object required error?
0
Comment
Question by:ksummers
9 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20025102
Assuming you are posting part of a SCRIPT rather than trying to do C#, both look perfectly reasonable.

Can you post a bit more code?  I am guessing that you have identified the wrong line.
0
 

Author Comment

by:ksummers
ID: 20025188
Well, here is some of my code.  I had to edit it to take out some work related private stuff.  Basically what happens, is when the page loads there are some dropdowns to select search criteria.  I have a function hideGrid() to hide the grid when the dropdown is selected for a new search.  Well, when the page loads, the grid is empty and not there.  After the first search tho, the dropdowns work fine with the function because the grid is there because the element is found.  Hopefully that makes sense.  Here is some code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Viewer</title>
    <link rel="stylesheet" type="text/css" href="ReportViewerStyles.css" />
    <SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></SCRIPT>
    <script language="javascript" type="text/javascript">
    <!--
function hideGrid()
{
if ( ! document.getElementById('gvReportInstances')) {      alert('grid is missing!');}
//I am trying to find the element so I can hide when the drop down is clicked for a new search.                                      
//document.getElementById("gvReportInstances").style.display = "none";
//document.getElementById("btnNewSearch").style.display = "none";
}
    //-->
</script>
<body>
    <form id="Form1" runat="server">
..
..
..
    <div id="ScrollDiv" style="border : solid 0px #000000; background : #ffffff; color : #000000; padding : 0px; width : 100%; height : 350px; overflow : auto; ">
                                            <asp:GridView ID="gvReportInstances" runat="server" HorizontalAlign="Center" Width="775px"
                                                PageSize="25" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
                                                CellPadding="0" EmptyDataText="No reports meeting the specified criteria were found.  Modify the search criteria and try again."
                                                BorderStyle="None" BorderWidth="0px" CaptionAlign="Bottom" OnPageIndexChanging="gvReportInstances_PageIndexChanging" OnRowCommand="gvReportInstances_RowCommand" DataKeyNames="InstanceKey,FileName,OfflineZipFile">
                                                <HeaderStyle CssClass="SubMenu" BorderStyle="None" />
                                                <AlternatingRowStyle BackColor="AliceBlue" BorderStyle="None" BorderWidth="0px" />
                                                <Columns>
                                                  <asp:ButtonField ButtonType="Image" ImageUrl="~/Resources/mag_glass.gif" Text="View" CommandName="View" />
                                                    <asp:TemplateField HeaderText="Client">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblClientName" runat="server" Text='<%# Eval("ClientName")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Run Date">
                                                        <ItemTemplate>
                                                            <asp:Label ID="RunDate" runat="server" Text='<%# Eval("RunDate")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Parameters">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Parameters" runat="server" Text='<%# Eval("ParamName")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                                <RowStyle BorderStyle="None" />
                                                <EditRowStyle BorderStyle="None" />
                                                <SelectedRowStyle BorderStyle="None" HorizontalAlign="Left" />
                                            </asp:GridView>
                           
                                            <asp:Label ID="MaxRowLabel" runat="server" ForeColor="Red" Width="584px"></asp:Label>
                                            </div>
 </form>
0
 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 248 total points
ID: 20025278
First, right click in the browser and choose View Source for the page in question. Find the HTML element corresponding to the GridView and be sure that the ASP.NET id you gave the control is the same as the HTML id. ASP.NET sometimes changes the value so you may not be finding the element in the DOM.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 252 total points
ID: 20025344
There is NOTHING wrong with the hideGrid function as it stands....I cut and pasted it into a new html page, and it functioned....but if you did not have the last two lines in it commented OUT, those lines WOULD generate an "object required" error.

If that is what was doing it, you want to put a RETURN statement inside your if...like:
if ( ! document.getElementById('gvReportInstances')) {      alert('grid is missing!'); return;}

otherwise you get the alert - and then STILL get the error.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20410961
I think it is VERY likely the error was originally caused by the lines commented out in the last code sample.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20413406
Considering that he SAID that the code was edited, did not assure us that the code as presented had actually been RUN - and that, with the code as IS, the error originally cited demonstrably does NOT occur, this seems the most probably cause to me.

Of course, when the Asker abandons the question, there is no way to tell for sure.
0
 

Author Comment

by:ksummers
ID: 20537189
I can't remember exactly why we ended up not going with the JavaScript when the page loaded.  I think because the grid was not visible 'til AFTER a search was done, so we kept getting the error in the page_load.  Basically, I think we modified the datasource of the grid whenever the dropdown was selected like this:

    gvReportInstances.DataSource = null;
    gvReportInstances.DataBind();

And the grid disappeared.  I can't remember because this was a few months ago.  Sorry I did not keep up with the thread, but you guys were great.  Thanks.
0
 

Author Comment

by:ksummers
ID: 20538274
b0lsc0tt, thank you.  sorry about that.  you were included in the split when i checked multiple solutions accepted.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20561978
Forced accept as detailed in http:#20537502 .

b0lsc0tt
EE Zone Advisor
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

829 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