Skip to content

Instantly share code, notes, and snippets.

@rch850
Last active December 10, 2015 02:58
Show Gist options
  • Save rch850/4371373 to your computer and use it in GitHub Desktop.
Save rch850/4371373 to your computer and use it in GitHub Desktop.
A jQuery plug-in to switch the action of forms by a select element.
/**
* A jQuery plug-in to switch the action of forms by a select element.
* Each forms must have data-api attribute.
*
* EXAMPLE:
*
* <select name="actionBase">
* <option value="http://localhost/">localhost</option>
* <option value="http://dev.example.com/">dev server</option>
* </select>
* <form data-api="users/name" method="post">...</form>
* <form data-api="users/email" method="post">...</form>
* <script>
* $("select[name=actionBase]").enableAutoSwitch();
* </script>
*/
(function($) {
$.fn.enableActionSwitch = function() {
return this.each(function() {
var $this = $(this);
function setActionBase() {
$("form").each(function() {
$(this).attr("action", $this.val() + $(this).data("api"));
});
}
$this.on("change", function() {
setActionBase();
});
setActionBase();
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment