Solved

Default button for Enter key

Posted on 2004-10-29
1,720 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
Question by:AnithaB
    5 Comments
     
    LVL 15

    Expert Comment

    by:justinbillig
    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
    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:
    @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
    @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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    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…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now