Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Default button for Enter key

Posted on 2004-10-29
7
Medium Priority
?
1,722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

597 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