Avatar of MikeMCSD
MikeMCSD
Flag for United States of America asked on

Error when Enter is pressed for Search in IE8

After typing in  a search word and pressing Enter:
Message: 'document.getElementById(...)' is null or not an object
Line: 118  (below)    /default.aspx

<input name="ctl00$wpm$HomePage$ctl00$SearchPhrase" type="text"
id="ctl00_wpm_HomePage_ctl00_SearchPhrase"
onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('ctl00$wpm$HomePage$ctl00$SearchButton').click();return false;}} else {return true}; " />

<input type="submit" name="ctl00$wpm$HomePage$ctl00$SearchButton"  
onclick="if(Page_ClientValidate('Search')){window.location='/Search.aspx?k='+encodeURIComponent(ctl00_wpm_HomePage_ctl00_SearchPhrase.value);}return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$wpm$HomePage$ctl00$SearchButton&quot;, &quot;&quot;, true, &quot;Search&quot;, &quot;&quot;, false, false))"
id="ctl00_wpm_HomePage_ctl00_SearchButton"  />

Do the dollar signs have anything to do with it:

ctl00$wpm$HomePage$ctl00$SearchButton
ctl00_wpm_HomePage_ctl00_SearchButton  << id of Search button
JavaScript

Avatar of undefined
Last Comment
Mangagm

8/22/2022 - Mon
Mangagm

yes, your function tries to click the button with id "ctl00_wpm_HomePage_ctl00_SearchButton" but you are trying to get the element by the Id ctl00$wpm$HomePage$ctl00$SearchButton, switch:

<input name="ctl00$wpm$HomePage$ctl00$SearchPhrase" type="text"
id="ctl00_wpm_HomePage_ctl00_SearchPhrase"
onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('ctl00$wpm$HomePage$ctl00$SearchButton').click();return false;}} else {return true}; " />

for:

<input name="ctl00$wpm$HomePage$ctl00$SearchPhrase" type="text"
id="ctl00_wpm_HomePage_ctl00_SearchPhrase"
onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('ctl00_wpm_HomePage_ctl00_SearchButton ').click();return false;}} else {return true}; " />
MikeMCSD

ASKER
I wish it was easy as that. It's created like this:

<asp:TextBox ID="SearchPhrase" runat="server"></asp:TextBox>

    string handleEnterScript = "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"
        + SearchButton.UniqueID + "').click();return false;}} else {return true}; ";

    SearchPhrase.Attributes.Add("onkeydown", handleEnterScript);

    string searchUrl = this.Page.ResolveUrl("~/Search.aspx");

    SearchButton.OnClientClick = string.Format(SearchButton.OnClientClick, searchUrl, SearchPhrase.ClientID);


Mangagm

you got an error here:

string handleEnterScript = "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"
        + SearchButton.UniqueID + "').click();return false;}} else {return true}; ";

this part: 13)) {document.getElementById('"

is closing the string, try this:

var handleEnterScript = function(){ if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"
        + SearchButton.UniqueID + "').click();return false;}} else {return true}; ";
}
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
MikeMCSD

ASKER
thanks, . . it doesnt' give the error anymore, but it doesn't go
to the search page.
ASKER CERTIFIED SOLUTION
Mangagm

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
MikeMCSD

ASKER
that would probably do it too,
but this also worked:

document.getElementById('"+  SearchButton.ClientID + "')

thanks for your help.

Mangagm

Anytime, thx for the points.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.