SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    florida
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to control Enter key action in my form

    On one of my site web pages, I have a form that has an input area, a button and several check boxes all strung out in a line along the top of my Tips access page. The input area is where you enter your search argument, the check boxes are how you control what areas of the tips database to search (none checked means search all of them) and the button, labeled Search, kicks off the search.

    What I want to do and can't remember how, is to be able to enter a search string in the input area and hit enter and have the default action be as if I had clicked on the Search button.

    Any html programmers here who can tell me how to do this or where I can go to look it up?

    I believe I can do this by re-arranging the order so the Search button is first but that doesn't look right nor make sense from a useability point of view.

    If there is a better place to ask this question, please point me to it.

    Thanks,

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should work automatically as long as you specify type="submit" for the button:
    HTML Code:
    <input type="submit" value="Search">
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    Aug 2004
    Location
    florida
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't, I use type=button as I have JavaScript I call when the button is clicked on that does some checking and does the submit if all is okay, otherwise it returns false to prevent the submit from taking place.

    I did find an answer today though. I used the onKeydown function in the text box definition, works perfectly.

    <input type=text class=txt1x name=search_data onKeydown="whatKey()">

    Here is the JS function:

    function whatKey()
    {
    var form = document.forms[0]
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
    var keycode;
    if (window.event)
    keycode = window.event.keyCode;
    else
    if (e)
    keycode = e.which;
    if(keycode == 13)
    if (checkInput('showtipsbysearch.html?view=$view&home=$home&show_private=$showprivate'))
    return true
    else
    return false
    }
    }

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A better solution would be to put the validation in the form's onsubmit handler and use a proper submit button.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Member
    Join Date
    Aug 2004
    Location
    florida
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, then the whatkey routine wouldn't get invoked for evey key press. I'll take a look at that but it's hard to find enthusiasm to change something that is working so well :-), even if it is the right thing to do.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's understandable. But with this knowledge, you can create a better solution next time you need to create a form with validation-on-submit.
    Birnam wood is come to Dunsinane


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
  •