Skip to content

HTML form with two submit buttons and two “target” attributes

I have one HTML <form>.

The form has only one action="" attribute.

However I wish to have two different target="" attributes, depending on which button you click to submit the form. This is probably some fancy JavaScript code, but I haven’t an idea where to begin.

How could I create two buttons, each submitting the same form, but each button gives the form a different target?


It is more appropriate to approach this problem with the mentality that a form will have a default action tied to one submit button, and then an alternative action bound to a plain button. The difference here is that whichever one goes under the submit will be the one used when a user submits the form by pressing enter, while the other one will only be fired when a user explicitly clicks on the button.

Anyhow, with that in mind, this should do it:

<form id='myform' action='jquery.php' method='GET'>
    <input type='submit' id='btn1' value='Normal Submit'>
    <input type='button' id='btn2' value='New Window'>

With this javascript:

var form = document.getElementById('myform');
form.onsubmit = function() { = '_self';

document.getElementById('btn2').onclick = function() { = '_blank';

Approaches that bind code to the submit button’s click event will not work on IE.