• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1724
  • Last Modified:

Default button for Enter key

Hi,

I'm trying to force a default button for Enter  key. I'm using the following code

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

 
  }
 }

  else if(document.layers)
 {
  if(event.which == 13)
  {
   event.returnValue=false;
   event.cancel = true;
   document.getElementById('btnContinue'.ClientID).click();

 
  }

 }

}

</script>
</HEAD>
      <body onkeydown= fnTrapKeyDown(event);>

btnContinue is the image button on which click event to be raised.

This script works fine in IE ,but not on Netscape7.0

Can you please tell me what is wrong in this.

Anitha
0
AnithaB
Asked:
AnithaB
  • 2
  • 2
1 Solution
 
justinbilligCommented:
try this


function fnTrapKeyDown( e )
{
      if( e.keyCode == 13 )
      {
            event.returnValue = false;
            event.cancel = true;
            document.getElementById( 'btnContinue' ).click( );
      }
}
0
 
justinbilligCommented:
or this



function fnTrapKeyDown( e )
{
      var intKeyPressed = 0;

      // Which Broswer?
      if( document.all )
      {
            intKeyPressed = event.keyCode;
      }
      else
      {
            intKeyPressed = e.which;
      }

      
      if( intKeyPressed  == 13 )
      {
            event.returnValue = false;
            event.cancel = true;
            document.getElementById( 'btnContinue' ).click( );
      }
}
0
 
dakydCommented:
@justin
  I don't think that'll work because Netscape doesn't recognize a global event object.  Thus, event.returnValue and event.cancel will be undefined in Netscape.

AnithaB,
  Will something like the following work?  I have a "Continue" button that simply alerts "clicked", but you can change it to suit your purposes.  Either way, the document captures the Enter press and turns it into a click of the "Continue" button.  Hope that helps.

<html>
<head>
<script type="text/javascript">
document.onkeydown = fnTrapKeyDown;

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

  if(intKeyPressed == 13)
  {
    document.getElementById('btnContinue').click();
    return false;
  }
}
</script>
</head>

<body>
<input type="button" id="btnContinue" value="Continue" onclick="alert('clicked');" />
</body>
</html>
0
 
AnithaBAuthor Commented:
@dakyd


Unfortunately this is also not working in Netscape. The problem is I'm using Image buttons and Netscape is not able to recognise that.

Any other solution.?

Thanks
Anitha
0
 
dakydCommented:
You're using an image button as the "Continue" button?  Is the image submitting to a form, or does it have an onclick handler that goes to the next page?  Is your page online?  If not, can you post the HTML so we can tweak it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now