SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    There can be only One.... joelwills's Avatar
    Join Date
    Jul 2000
    Location
    England
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OnClick Submit problem

    Hi All,

    I have button in a form that uses onClick to do what is required. This all works fine, if the user clicks the button. However, if they press the Enter key, then the page just refreshes itself and doesn't perform the required action.

    This occurs in both IE and NN. Is there any way to process the enter key the same as clicking the button?

    Many thanks,

    Joel
    Life Goggles - The Entertaining Environment Blog
    http://www.lifegoggles.com

  2. #2
    SitePoint Guru nagrom's Avatar
    Join Date
    Jul 2001
    Location
    Western CT, USA
    Posts
    803
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is this button the submit button? if so, move your javascript to the onsubmit attribute of the form itself.

  3. #3
    There can be only One.... joelwills's Avatar
    Join Date
    Jul 2000
    Location
    England
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it's just a normal button however.

    I will attempt to move things around a bit to see if I can get it to work that way.

    Many thanks,

    Joel
    Life Goggles - The Entertaining Environment Blog
    http://www.lifegoggles.com

  4. #4
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you like to post some code. It is much easier to help someone when we can view either a) the source or b) the site with the error
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the onSubmit attribute in the form tag instead of onClick. The onSubmit attribute does anything immediately after the form is submitted.

    It is used like this:

    <form method='POST' action='formmail.php' onSubmit='onClick stuff here'>

    // form here

    </form>

    This is way better, because if they do press the enter key, the onClick doesn't take effect. But with onSubmit, it does.

  6. #6
    There can be only One.... joelwills's Avatar
    Join Date
    Jul 2000
    Location
    England
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Maelstrom, but I've solved it using nagrom's suggestion. I converted the button to a submit one and moved the javascript to the action part of the form tag.

    Cheers!
    Life Goggles - The Entertaining Environment Blog
    http://www.lifegoggles.com

  7. #7
    There can be only One.... joelwills's Avatar
    Join Date
    Jul 2000
    Location
    England
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks too gregc, we must have posted at the same time almost!

    Life Goggles - The Entertaining Environment Blog
    http://www.lifegoggles.com


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
  •