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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    188
    Thanks
    141
    Thanked 0 Times in 0 Posts

    Enter Key to Trigger Custom Button

    Hello,

    I need your help.

    I basically need, when the enter button is pressed on the keyboard to trigger a button. Currently, my code works but it just refreshes the page and does not actually trigger the desired button:

    Code:
    //HERES MY CUSTOM BUTTON:
    
    <BUTTON TYPE="submit" 
    onMouseOver="goLite(this.name)"
    onMouseOut="goDim(this.name)"
    onclick="javascript:v7_search()"
    CLASS="btn_std"
    NAME="btn_search"
    >&nbsp;? Search
    
    </BUTTON>
    Here's the code to make the enter button work:

    Code:
    <form name="frmIMTS" 
    onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('btn_search').click();}};"
    >
    Any help with this is greatly appreciated.

    Cheers,

    J

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Yes, it really does trigger the button.

    But because the button is type="submit" the normal action of the button is, indeed, to submit the <form> containing it to the server. Which then quite naturally reloads the HTML page, as it is supposed to.

    If you don't want this to be a submit button, then don't make it one.

    If all you really are trying to do is invoke the function specified by the onclick, then do that, instead of invoking click on the function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    188
    Thanks
    141
    Thanked 0 Times in 0 Posts
    Thanks Old Pendant.

    A little progress here, however, I still cant get the button to fire when the user hits enter

    here is my code:

    Code:
    <html>
    
    <head>
    
    <SCRIPT TYPE="text/javascript">
    
    function test(){
    
    alert("testing123")
    
    }
    
    </script>
    
    
    </head>
    
    <body>
    
    <form name="frmIMTS" 
    onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('btn_search').click();}};"
    >
    <BUTTON
    onclick="javascript:test()"
    NAME="btn_search"
    >PRESSME
    </BUTTON>
    </form>
    
    
    </body>
    
    </html>
    A hopeless soul needs your help.

    Much thanks and appreciation in advance.

    J


  •  

    Posting Permissions

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