Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Textbox Enter Key Issue

    I have a textbox on my webpage where you can enter a title. I then have a button that goes to my javascript and starts a timer. Well if the user presses enter on the text box I want it to start the time. Well it goes to the code, but then like refreshes the website and starts from the start. How can I get around this? When I click the button there is no issues at all.

    Textbox:

    <input type="text" id="titleMainT"
    value="N/A" size="27" onkeypress="startTimer();"/>

    Button:

    <input type="button" id="startbutton" value="Start Timer" onclick="startTimer();" />

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    onkeypress will trigger the script each time a key is pressed.
    Try onblur instead.

    If your problem is that pressing the enter key causes a form to submit, the the solution is to disable the enter key:-
    onkeypress = " return !(window.event && window.event.keyCode == 13)";


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 07-30-2011 at 08:28 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    onblur resets it as well.

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it also puts this at the end of my web address...

    ?selectfamily=Arial%2C+Helvetica%2C+sans-serif&selectFont=30&selectcolorBACKGROUND=WHITE&selectcolorFONT=BLACK

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yea, my issue is the form submits. I just want them to process the website by the button. Is there a chance I could fix that and make sure when they press enter on the textbox it does nothing?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by tvb2727 View Post
    Yea, my issue is the form submits. I just want them to process the website by the button. Is there a chance I could fix that and make sure when they press enter on the textbox it does nothing?
    Yes, disable the enter key as I indicated in Post #2.

    A more extended script which may be useful if there are multiple input fields is

    Code:
    <script type = "text/javascript">
    
    function onKeyPress (e) {
    var keycode;
    if (window.event) {keycode = window.event.keyCode}
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13) {
    alert ("Please use the tab key (not the enter key) or click\nwith your mouse to move between the form fields.");
    return false;
    }
    return true; 
    }
    
    document.onkeypress = onKeyPress;
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    tvb2727 (07-30-2011)

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You just fixed my issue. Thanks a lot!


  •  

    Posting Permissions

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