RE: quick js question about calling functions

Hi Alex,

It's possible to trap key press events and perform an action based on the
key code.  In it's simplest form I've used something along these lines:

function keyCheck(e)
{
  var result = false;
  var keyCode = 0;

  if (window.event) // for IE
  {
    keyCode = e.keyCode;
  }
  else if (e.which) // for Netscape, etc...
  {
    keyCode = e.which;
  }

  switch (keyCode)
  {
    case 13:
    {
      myHandler();
      break;
    }

    default:
    {
      result = true;
    }
  }

  return result;
}

function myHandler()
{
  // Do something...
}

...and the HTML:

<input type="text" name="myText" value="" onkeypress="return
keyCheck(event)"/> or <textarea name="myText" rows="2" cols="30"
onkeypress="return keyCheck(event)"></textarea>

There is plenty of good examples around and these can be found by giving
Google a tickle and searching for onkeypress, javascript, event handling,
etc as keywords.  BTW it might also be worth checking out the onblur and
onchange event handlers as well.

HTH :)

Cheers.
AB

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Saturday, 18 April 2009 2:22 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: E: quick js question about calling functions

I would like to call the function when the enter key is pressed 
in an input box (input type="text").  I know about how to do it 
with buttons and links, but not with textboxes; it should not be 
a clickable object; I just want the user to be able to hit enter 
and have the function called.  Thanks.

Have a great day,
Alex

> ----- Original Message -----
>From: "Adrian Beech" <a.beech@xxxxxxxxxxxxxx
>To: <programmingblind@xxxxxxxxxxxxx
>Date sent: Sat, 18 Apr 2009 11:07:01 +1000
>Subject: RE: quick js question about calling functions

>G'day Alex,

>Yup it certainly can be done.  For clickable objects such as 
buttons the tag
>which I use goes along the lines of <input type="button" 
value="Click Me"
>onclick="myFunction()"/> and with hyperlinks I use <a
>href="javascript:myFunction()">Click Me</a>.  It's also generally 
good
>practice to put the functions in a separate .js file and add a 
<script
>type="text/javascript" src="my_functions.js"></script> within the 
head
>section of your page.

>HTH.

>Cheers.
>AB


>-----Original Message-----
>From: programmingblind-bounce@xxxxxxxxxxxxx
>[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex 
Hall
>Sent: Friday, 17 April 2009 11:37 PM
>To: programmingblind@xxxxxxxxxxxxx
>Subject: quick js question about calling functions

>Hi all,
>Just a quick question: in a webpage, I have a textbox.  I would
>like the user to be able to hit the ENTER key in this box and
>call a function, instead of needing to click a button.  The page
>is a simple game meant to be played offline, so the page should
>not go anywhere or reload itself.  Basically, I want the enter
>key inside a textbox to take the place of a button.  Can this be
>done?

>Have a great day,
>Alex
>__________
>View the list's information and change your settings at
>http://www.freelists.org/list/programmingblind



>__________ NOD32 4017 (20090417) Information __________

>This message was checked by NOD32 antivirus system.
>http://www.eset.com


>__________
>View the list's information and change your settings at
>http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind



__________ NOD32 4018 (20090418) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com


__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

Other related posts: