Change Form Action with Javascript

G

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
 
D

David Dorward

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.
 
V

VK

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>
 
R

Randy Webb

VK said the following on 12/21/2005 5:59 AM:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top