jyothsna1803
asked on
Default button for Enter Key
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('b tnSearch') .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('b tnClick'). click();
return false;
}
}
This is not firing at all. What is the problem, Can you please help me out.
document.onkeydown = fnTrapKeyDown;
function fnTrapKeyDown(e)
{
var intKeyPressed = document.all? event.keyCode: e.which;
if(intKeyPressed == 13)
{
document.getElementById('b
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('b
return false;
}
}
This is not firing at all. What is the problem, Can you please help me out.
>> 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(ob jId).click ();
return false;
}
}
OK, then when you do this =>document.onkeydown = fnTrapKeyDown('btnClick');
document.onkeydown = fnTrapKeyDown(window.event
function fnTrapKeyDown(e, objId )
{
var intKeyPressed = document.all? event.keyCode: e.which;
if(intKeyPressed == 13)
{
document.getElementById(ob
return false;
}
}
ASKER
No, it is not recognising document.onkeydown = fnTrapKeyDown(window.event , 'btnClick'); event. if i click enter then no response from the browser.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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