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('f oo')) {alert('foo is missing!');}
Is there another way to check for this id foo which will work and not return the object required error?
if (document.getElementById('
//or:
if ( ! document.getElementById('f
Is there another way to check for this id foo which will work and not return the object required error?
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.c ss" />
<SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></S CRIPT>
<script language="javascript" type="text/javascript">
<!--
function hideGrid()
{
if ( ! document.getElementById('g vReportIns tances')) { 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( "gvReportI nstances") .style.dis play = "none";
//document.getElementById( "btnNewSea rch").styl e.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="gvRep ortInstanc es_PageInd exChanging " OnRowCommand="gvReportInst ances_RowC ommand" DataKeyNames="InstanceKey, FileName,O fflineZipF ile">
<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")%>'></as p:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Date">
<ItemTemplate>
<asp:Label ID="RunDate" runat="server" Text='<%# Eval("RunDate")%>'></asp:L abel>
</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>
<!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.c
<SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></S
<script language="javascript" type="text/javascript">
<!--
function hideGrid()
{
if ( ! document.getElementById('g
//I am trying to find the element so I can hide when the drop down is clicked for a new search.
//document.getElementById(
//document.getElementById(
}
//-->
</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="gvRep
<HeaderStyle CssClass="SubMenu" BorderStyle="None" />
<AlternatingRowStyle BackColor="AliceBlue" BorderStyle="None" BorderWidth="0px" />
<Columns>
<asp:ButtonField ButtonType="Image" ImageUrl="~/Resources/mag_
<asp:TemplateField HeaderText="Client">
<ItemTemplate>
<asp:Label ID="lblClientName" runat="server" Text='<%# Eval("ClientName")%>'></as
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Date">
<ItemTemplate>
<asp:Label ID="RunDate" runat="server" Text='<%# Eval("RunDate")%>'></asp:L
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parameters">
<ItemTemplate>
<asp:Label ID="Parameters" runat="server" Text='<%# Eval("ParamName")%>'></asp
</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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
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.
Of course, when the Asker abandons the question, there is no way to tell for sure.
ASKER
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.DataSour ce = 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.
gvReportInstances.DataSour
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.
ASKER
b0lsc0tt, thank you. sorry about that. you were included in the split when i checked multiple solutions accepted.
Can you post a bit more code? I am guessing that you have identified the wrong line.