Change Form Action with Javascript

Discussion in 'Javascript' started by ANTISPAM_garycnew_ANTISPAM@yahoo.com, Dec 21, 2005.

  1. Guest

    I am trying to figure out the best way to change a form's action based
    on a single text input event.

    I have a search form within a larger form and would like to allow users
    to use the enter key within the search text input without submitting
    the larger form. I discovered the onKeyPress event and the keyCode for
    the Enter Key (13). Now I need to figure out a way to pass the result
    of the onKeyPress to the larger form's onSubmit event to return false
    if the onKeyPress is true or allow the larger form to submit if false.

    What's the easiest way to pass the onKeyPress value to the onSubmit
    event?

    if (event.keyCode == 13) {
    document.getElementById('myform').action = "javascript: return
    false;";
    }

    or

    if (event.keyCode == 13) {
    document.forms.element.hiddentextinput.value = "javascript: return
    false;";
    }

    Thank you for your assistance.

    Respectfully,


    Gary
    , Dec 21, 2005
    #1
    1. Advertising

  2. wrote:

    > I am trying to figure out the best way to change a form's action based
    > on a single text input event.


    To ... "not". Have your server side form handler redirect as appropriate.

    JavaScript is a fantastic tool for making life easier for users, but
    depending on it isn't such a great idea.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Dec 21, 2005
    #2
    1. Advertising

  3. VK Guest

    wrote:
    > I am trying to figure out the best way to change a form's action based
    > on a single text input event.


    If form contains only one <input type="text" element then pressing
    Enter in this input leads to the form submission. This is a convenience
    behavior, but if it becomes an inconvenience then you can turn it off
    by using HTML only - no script needed.

    Simply add another <input type="text" into your form.

    Also you can use textarea instead of textfield:
    <textarea name="out" cols="20" rows="1"></textarea>
    VK, Dec 21, 2005
    #3
  4. Randy Webb Guest

    VK said the following on 12/21/2005 5:59 AM:
    > wrote:
    >
    >>I am trying to figure out the best way to change a form's action based
    >>on a single text input event.

    >
    >
    > If form contains only one <input type="text" element then pressing
    > Enter in this input leads to the form submission. This is a convenience
    > behavior, but if it becomes an inconvenience then you can turn it off
    > by using HTML only - no script needed.
    >
    > Simply add another <input type="text" into your form.


    The <enter> key will still submit the form though.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Dec 21, 2005
    #4
  5. VK Guest

    Randy Webb wrote:
    > The <enter> key will still submit the form though.


    Only if the browser is badly borken, thus not in IE or FF. What browser
    did you try?
    VK, Dec 21, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tim payne

    change server side form action

    tim payne, Apr 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    tim payne
    Apr 6, 2004
  2. Joe Bloggs
    Replies:
    1
    Views:
    752
    Sudsy
    Aug 3, 2003
  3. John
    Replies:
    0
    Views:
    860
  4. Replies:
    7
    Views:
    624
  5. Replies:
    2
    Views:
    216
Loading...

Share This Page