Dynamic Form Actions using Different Buttons

Multithreaded JavaScript has been published with O'Reilly!
DEPRECATED: This post may no longer be relevant or contain industry best-practices.

For my NeoInvoice project, we had a need to change the form action based on which button is being pressed. Sure, we could have had the resulting URL determine which action to take based on the clicked button name in the POST array, but we have a specific URL nomenclature to maintain.

This simple solution uses JavaScript to check which button was pressed, thereby changing the form action right before the form gets submitted. This solution has been tested in both Firefox and IE8.

<form method="post">
    <input type="text" name="items" /><br />
    <input type="submit" value="Delete" onClick="this.form.action='/action/delete';" /> |
    <input type="submit" value="Touch" onClick="this.form.action='/action/touch';" /><br />
</form>
Thomas Hunter II Avatar

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript and is an O'Reilly published author.