Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Change Form Action with Javascript

Reply
Thread Tools

Change Form Action with Javascript

 
 
ANTISPAM_garycnew_ANTISPAM@yahoo.com
Guest
Posts: n/a
 
      12-21-2005
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

 
Reply With Quote
 
 
 
 
David Dorward
Guest
Posts: n/a
 
      12-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      12-21-2005

(E-Mail Removed) 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>

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-21-2005
VK said the following on 12/21/2005 5:59 AM:
> (E-Mail Removed) 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/
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      12-21-2005

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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts: 1 action, 1 Form, several views handled by 1 only action. Any idea for better design? John Java 0 06-26-2007 11:22 PM
javascript form validation - form action to another asp page iam247@gmail.com ASP General 7 08-20-2005 10:06 PM
javascript form action = send form details to ASP page iam247@gmail.com Javascript 2 08-07-2005 08:16 AM
change server side form action tim payne ASP .Net 0 04-06-2004 02:36 PM
Struts Static Action Form vs Dyanamic Action Form Joe Bloggs Java 1 08-03-2003 02:30 AM



Advertisments