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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    <input type=text... want Enter to call a function

    I'm using onChange and it works fine. But I can't figure out how to also have the user's typing Enter after changing the text also call a function.

    This has got to be an easy one but I'm stumped. Looked in my book and even at the search <input> box for THIS website! It uses type=hidden if that makes a difference.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://developer.irt.org/script/2066.htm

    ,just add your function to the onsubmit:

    onSubmit="myfunc(); return false">

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is my code and the function setSearchText is called fine by changing the text and then clicking anywhere on the page (onChange). But changing the text and then hitting the Enter key is not doing anything (onSubmit).

    c+="<input type=text name=search value='"+searchText+"' onChange='setSearchText(this)' onSubmit='setSearchText(this); return false' size=10 maxlength=25 border=1>";

    I'm so ignorant that I stumble on the littlest things... which is why I love this forum.

    Thanks!

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I always see the onSubmit event called in the <form> I'm not sure if you can call the event from a text box.

    <form onSubmit='setSearchText(this); return false'>
    <input ...>
    </form>

    That's something to try at least.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    onsubmit isn't an event for an input tag. You should use the onkey, onkeydown or onkeyup events instead. Then you could also get rid of the onchange event code since any changes would already be handled.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    arnyinc, it's already inside a <form> but I tried your code anyway and it didn't help.

    Roy, it's a search function and you wouldn't want the function to be called until all the text was entered. I have a "search" button beside it but would also like it to work just like the search feature at this forum where you can EITHER click on GO or just hit Enter.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You still need to use one of the onkey events to do what you want, you just have to examine the key the user entered and only trigger your function when it's the enter key.

    Essentially you want to do the opposite of the function requested in this thread http://www.codingforums.com/showthre...threadid=7492.

    The hooks you need to use are the same so this code would take little altering to do what you want.

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function checkEnter(e) {
    var kC = window.event ? event.keyCode :
    e && e.keyCode ? e.keyCode :
    e && e.which ? e.which : null;
    if (kC) return kC == 13;
    else return false;
    }

    <input.......onkeypress="if(checkEnter(event))setSearchText(this)">

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't have to do any of that ... providing that your form has a real submit button:

    <input type="submit">

    or

    <input type="image">

    then pressing enter from an input box will always submit the form, and trigger the function called in <form onsubmit.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Roy, I tried that and got the Enter key working easily. Thanks.

    Now I have a different problem. I am trying to call the function Search() which works well from a button, but gives the error message "object expected" from my keyCheck() function. Is there a problem calling one function from another?

    // Function to check for Enter key
    function checkKey(){
    if(event.keyCode==13){Search()}}

    c+="<input type=text name=search value='"+searchText+"' onChange='setSearchText(this)' onKeyDown='checkKey()' size=10 maxlength=25 border=1>";

    cheesebagpipe and brothercake... I'm trying to us the Enter key to call the Search() function so you don't have to click on the Search button.

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function checkKey(){ 
    if(event.keyCode==13){Search()}}
    I assume the "Search" function calls the setSearchText function. Just make sure you've got the proper object reference to the input field of your form since that's how it's called in the onchange event call.


  •  

    Posting Permissions

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