SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Telling if keys been hit

    Is there anyway to tell what keyboard key has been pressed in a textarea? I need to tell when someone has hit the return key.

    Thanks.


  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use this script in the head section:
    <SCRIPT LANGUAGE="JavaScript">
    function dofunc(e)
    {
    if (document.all)
    {
    var keycodec = event.keyCode
    }
    else
    {
    var keycodec = e.which
    }
    if(keycodec=='13') // checks for ascii code for enter key
    {
    return false; // So IE does not submit form when you hit the enter key in a form field
    }
    }
    </SCRIPT>

    Then have your textarea tag like this:
    <TEXTAREA ROWS="10" COLS="40" NAME="txta" onKeyDown="return dofunc(event)"></TEXTAREA>

    The onKeyDown event seems to work better for both IE and NS where as the onKeyPress acts differently for both browsers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •