Capturing enter key pressed using javascript.

Hi All,

I am using the following code to capture the enter key pressed.

function enterKeypressHandler(e)
{
      if (!e)
            e = window.event;
            
      if (e.keyCode == 13)
      {
            document.getElementById('<%=btnSearch.ClientID%>').click();
            return false;
      }
            
      return true;
}
document.attachEvent("onkeypress", enterKeypressHandler);

It works fine when I put this script right on the .aspx page.

When put this code in a .js file and call that .js file from .aspx then there is a problem.

This is how I am calling the script in .js file.

<script src="../js/enterKeypressHandler.js" type="text/javascript"></script>

problem is when I type something in a textbox and press enter does not work but if I remove the focus from the textbox then it works.

Can I what is causing the problem.

Any solution in this regard is appreciated.

Thanks,
SRM.
SRM19Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
Hi SRM19,
Perhaps you can amend the js's external link, eg:
<script src="js/enterKeypressHandler.js" type="text/javascript"></script>
0
SRM19Author Commented:
Hi x com,

I am doing that because the js file and the aspx file are under the same folder. they are in different folders.

Thanks,
SRM.
0
David H.H.LeeCommented:
Hi SRM,
Ok, that's meant your js file is pointing to correct file.
Can you post the js error details here? Perhaps you can put alert("test") to test whether the action has been invoked or not.

function enterKeypressHandler(e)
{
      if (!e)
            e = window.event;
           
      if (e.keyCode == 13)
      {
            alert("test") ;
            document.getElementById('<%=btnSearch.ClientID%>').click();
            return false;
      }
           
      return true;
}

0
SRM19Author Commented:
Hi x com,
Thanks for your responses

its definitely going into the javascript. i could get the alert message and then throwed the error

document.getElementById('...').click(); is null or not an object.

the problem  is when select something in a drop down and remove the focus from the drop down then it works. if the focus is still on drop down then it doesnt work.

Any idea why is it doing this.

Thanks,
SRM.
0
David H.H.LeeCommented:
Hi SRM,
Ok, if the existing JS code working fine before migrate the external JS source. Then, it could be something wrong with the passing parameters. Can i know what is the parameter that returned?

eg:
if (e.keyCode == 13)
      {
            alert('<%=btnSearch.ClientID%>') ;
            document.getElementById('<%=btnSearch.ClientID%>').click();
            return false;
      }

>>..the problem  is when select something in a drop down and remove the focus from the drop down then it works. if the focus is still on drop down then it doesnt work.
-Can you post some code snippet regarding the mentioned?


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.