?
Solved

Default button for Enter key

Posted on 2004-10-29
7
Medium Priority
?
1,724 Views
Last Modified: 2009-01-16
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
Comment
Question by:AnithaB
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 12444875
try this


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

Expert Comment

by:justinbillig
ID: 12444884
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
 
LVL 19

Accepted Solution

by:
dakyd earned 2000 total points
ID: 12450752
@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
 

Author Comment

by:AnithaB
ID: 12463479
@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
 
LVL 19

Expert Comment

by:dakyd
ID: 12465124
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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

592 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