View Full Version : dynamic action & submit button outside of form

05-18-2012, 12:35 PM
Here is the code for test.html:


<script type="text/javascript">
function OnSubmitForm(){
document.myform.action ="insert.html";
return true;

<form name="myform" onsubmit="return OnSubmitForm()" method="get">
<input type='radio' name='Search' value='test'>test</input>

<a href='javascript:document.myform.submit()'><img src='images/buttonnext.jpg'></a>


The way I want it to work:
when the img (buttonnext.jpg) is clicked, the form is submitted. When the form is submitted it runs the javascript, which sets the action to be that insert.html is loaded. i.e. the end result should be to redirect to insert.html?Search=test

The way that it is working now:
the page is redirected to test.html?Search=test

Note: If I use a submit button that is part of the form (i.e. input field with type "submit") the page is redirected correctly.

What do I need to change to accomplish my goal?

05-18-2012, 03:47 PM
You are correct, when the form is submitted it is NOT running the javascript. That's pretty much the problem in a nutshell. I need to figure out why not.

The action needs to be defined in a javascript function because it is going to be dynamic (i.e. if x then go to one page, if y go to a different page).

It's odd to me that if the submit button is part of the form (input type submit) then everything works correctly. The href button I am using is definitely submitting the form, it just doesn't run the script. If both ways submit the form, why is only one way calling the script?

05-18-2012, 04:06 PM
<a href='javascript:if(OnSubmitForm())document.myform.submit()'>

05-18-2012, 04:20 PM
<a href='javascript:if(OnSubmitForm())document.myform.submit()'>

Perfect. Thanks!