Solved

Default button for Enter Key

Posted on 2008-06-23
5
1,707 Views
Last Modified: 2008-06-23
The below code is working fine for Default button for entry key.
document.onkeydown = fnTrapKeyDown;
function fnTrapKeyDown(e)
{
  var intKeyPressed = document.all? event.keyCode: e.which;

  if(intKeyPressed == 13)
  {
 
    document.getElementById('btnSearch').click();
    return false;
  }
}

But i want to implement the same functionality for many ASP pages, So i want to move fnTrapKeyDown function to common area and just
want to call document.onkeydown = fnTrapKeyDown and i would like to pass button name as parameter
document.onkeydown = fnTrapKeyDown('btnClick');
function fnTrapKeyDown(e)
{
  var intKeyPressed = document.all? event.keyCode: e.which;

  if(intKeyPressed == 13)
  {
 
    document.getElementById('btnClick').click();
    return false;
  }
}

This is not firing at all. What is the problem, Can you please help me out.
0
Comment
Question by:jyothsna1803
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:GaryRasmussen
ID: 21848293
Yu could do it ithis way.  Keep the EnterKeyWasHit function in a "common" area.

function EnterKeyWasHit(evt)      
{      evt = (evt) ? evt : event
      if (evt.which || evt.keyCode)
      {      if ((event.which == 13) || (event.keyCode == 13))
            {      return true
            }
      }
      else
      {      return false
      }
}

Then in your pages that are uniqe you can have these

function DoSomething(evt)
{      evt = (evt) ? evt : event
      if (EnterKeyWasHit(evt))
      {      alert ("Enter key was hit, Click button or call function")
      }
}

document.onkeydown = DoSomething
0
 
LVL 82

Expert Comment

by:hielo
ID: 21848732
>> i would like to pass button name as parameter
OK, then when you do this =>document.onkeydown = fnTrapKeyDown('btnClick'); you are passing a string, so e.which; is NOT an event object. You need to pass the event object AND the string:

document.onkeydown = fnTrapKeyDown(window.event, 'btnClick');
function fnTrapKeyDown(e, objId )
{
  var intKeyPressed = document.all? event.keyCode: e.which;

  if(intKeyPressed == 13)
  {
 
    document.getElementById(objId).click();
    return false;
  }
}
0
 

Author Comment

by:jyothsna1803
ID: 21849309
No, it is not recognising document.onkeydown = fnTrapKeyDown(window.event, 'btnClick'); event. if i click enter then no response from the browser.
0
 
LVL 1

Expert Comment

by:GaryRasmussen
ID: 21849424
I don't think you can pass parameters when you initialize your events. The only way I know of to do what you want to do (keeping the "WasEnterKeyHit" common to all other unique pages) is the way I posted in the first reply.
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21849432
try  this:
document.onkeydown = function(e){ fnTrapKeyDown(e, 'btnClick'); };

function fnTrapKeyDown(e, objId )
{
 var e = e ? e : window.event;
  var intKeyPressed = document.all? event.keyCode: e.which;

  if(intKeyPressed == 13)
  {
 
    document.getElementById(objId).click();
    return false;
  }
}
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question