akimran82 akimran82 - 4 years ago 79
Javascript Question

Form submit doesn't happen while opening the link in a new tab

I have a form with a javascript submit function.
I have 3 links (anchor tags) in the form & would like to set different values to the hidden parameter on submit based on the link clicked

Form submit works fine generally, but if I try to open the link in a new tab, form is not submitted. The alert within the javascript function is
not printed. Does anyone have any idea to fix this?

Below is the Sample Code :


<script type="text/javascript">
function submitForm(actionParam) {
alert("in submitForm");
document.getElementById("action").value = actionParam;
document.forms['myForm'].action = action;


<form name="myForm" id="myForm" method="post" action="/businesspanel">
<input type="hidden" id="action" name="action" value="" />

<a href="#" onclick="submitForm('action1');">Action 1</a></span>
<a href="#" onclick="submitForm('action2');">Action 2</a></span>
<a href="#" onclick="submitForm('action3');">Action 3</a></span>



Answer Source

Opening a link in a new window bypasses the JavaScript. Don't use links / JS to submit forms.

Just use submit buttons. You can even send your identifying values without involving JS.

<button name="action" value="action1">Action 1</button>
<button name="action" value="action2">Action 2</button>
<button name="action" value="action3">Action 3</button>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download