Link to home
Create AccountLog in
Avatar of ksummers
ksummers

asked on

GetElementById find element/exists Javascript?

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?
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

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.
Avatar of ksummers
ksummers

ASKER

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>
SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I think it is VERY likely the error was originally caused by the lines commented out in the last code sample.
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.
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.
b0lsc0tt, thank you.  sorry about that.  you were included in the split when i checked multiple solutions accepted.
Forced accept as detailed in http:#20537502 .

b0lsc0tt
EE Zone Advisor