Link to home
Create AccountLog in
Avatar of MikeMCSD
MikeMCSDFlag 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
Avatar of Mangagm
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}; " />
Avatar of 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);


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}; ";
}
thanks, . . it doesnt' give the error anymore, but it doesn't go
to the search page.
ASKER CERTIFIED SOLUTION
Avatar of Mangagm
Mangagm

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
that would probably do it too,
but this also worked:

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

thanks for your help.

Anytime, thx for the points.