Why am I getting the message "Can't read the 'click' property of a null error

Inside the tag BODY

:

<asp:Panel ID="nmSearch" CssClass="searchBox" runat="server" DefaultButton="HiddenSearchNM">
    <input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
    <a href="JavaScript:void(0);" onclick="SearchNMClick();" title="Search" class="styledBtnSearch searchAnchor floatLeft defaultLinks">
        <asp:Image ImageUrl="~/images/searchWhite.png" CssClass="searchImg" runat="server" ToolTip="Search" AlternateText="Search" />
    </a>
    <asp:ImageButton ID="HiddenSearchNM" runat="server" CssClass="hideContent" ClientIDMode="Static" />
</asp:Panel>

      

Inside the tag HEAD

:

<script>
function SearchNMClick() {
    document.getElementById('HiddenSearchNM').click();
}
</script>

      

I see the following error in the console:

Uncaught TypeError: Cannot read property 'click' of null
SearchNMClick
onclick

      

C # code behind which the search page will be removed from Enter

or press:

protected void HiddenSearchNM_Click(object sender, EventArgs e)
    {
        //MessageBox.Show("SEARCH NM");
        strSMain = searchB.Value;
        Response.Redirect("results.aspx?searchtext=" + strSMain +"&folderid=0&searchfor=all&orderby=title&orderdirection=ascending");
    }

      

But when I press Enter while in a textbox or on a button, I get the above error.

How to resolve the error.

So weird, when I check the HTML source, I see this (not sure why the id is changing):

<input type="image" name="ctl00$CUSTOM_Area_Top$HiddenSearchNM" id="ctl00_CUSTOM_Area_Top_HiddenSearchNM" class="hideContent" ClientIDMode="Static" src="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$CUSTOM_Area_Top$HiddenSearchNM&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" style="border-width:0px;" />

      

+3


source to share


2 answers


You can change the JS function to:



function SearchNMClick() {
    document.getElementById('<%# HiddenSearchNM.ClientID %>').click();
}

      

+3


source


Use directive Page

to set static identifiers

<%@ Page ClientIDMode="static" %>

      



This should work with your id selector.

Also, check your rendered HTML for yours ImageButton

. If everything is configured correctly, it should save ID

.

+3


source







All Articles