...

View Full Version : Make "ENTER" key force a button to be clicked -- not working in FIREFOX



brookey86
04-21-2008, 06:50 PM
The function I have below is working great in IE, but it doesn't do anything in FireFox.

Any ideas why???



//THIS MAKES "ENTER" KEY CLICK THE "NEXT" BUTTON
//this is the html input tag:
//<input type="image" src="SurveyResource/next.gif" class="button" name="DATSTAT.NEXT" id="DATSTAT.NEXT" onClick="setFromButton('forward');" alt="Next >>">

function fnTrapKeyDown(e)
{
if (document.all)
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
document.getElementById("DATSTAT.NEXT").click();

}
}

else if(document.layers)
{
if(event.which == 13)
{
event.returnValue=false;
event.cancel = true;
document.getElementById("DATSTAT.NEXT".ClientID).click();
}
}
}




//here is the call
document.body.onkeydown=function(){

fnTrapKeyDown(event);
}

mjlorbet
04-21-2008, 10:17 PM
you need to call preventDefault() on your event object. also, the way you are using event is actually improper (in the function anyways, as firefox does not persist the event object through the chain of fired methods as ie does) you should be referencing it via e. instead of event.. so adding e.preventDefault() and e.cancelBubble = true to the end of a successful enter key press detection should get you closer. note that in opera, even if you do catch the enter key, if there is a form on the page the form will be submitted regardless (in my experience at least)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum