Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Make "ENTER" key force a button to be clicked -- not working in FIREFOX

    The function I have below is working great in IE, but it doesn't do anything in FireFox.

    Any ideas why???

    Code:
    //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();
    }
    }
    }
    Code:
    //here is the call
    document.body.onkeydown=function(){
    
    	fnTrapKeyDown(event);
    }

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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)
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •