$('#button1'). click(function(){ $('#your_form'). attr('action', 'http://uri-for-button1.com'); }); This code is the same for the second button, you only need to change the id of your button and the URI where the form should be submitted to.
form action javascript. In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.
Try this:
$('#button1').click(function(){
$('#formId').attr('action', 'page1');
});
$('#button2').click(function(){
$('#formId').attr('action', 'page2');
});
jQuery is just JavaScript, don't think very differently about it! Like you would do in 'normal' JS, you add an event listener to the buttons and change the action attribute of the form. In jQuery this looks something like:
$('#button1').click(function(){
$('#your_form').attr('action', 'http://uri-for-button1.com');
});
This code is the same for the second button, you only need to change the id of your button and the URI where the form should be submitted to.
Use jQuery.attr()
in your click handler:
$("#myform").attr('action', 'page1.php');
Please, see this answer: https://stackoverflow.com/a/3863869/2096619
Quoting Tamlyn:
jQuery (1.4.2) gets confused if you have any form elements named "action". You can get around this by using the DOM attribute methods or simply avoid having form elements named "action".
<form action="foo"> <button name="action" value="bar">Go</button> </form> <script type="text/javascript"> $('form').attr('action', 'baz'); //this fails silently $('form').get(0).setAttribute('action', 'baz'); //this works </script>
For variety's sake:
var actions = {input1: "action1.php", input2: "action2.php"};
$("#input1, #input2").click(function() {
$(this).closest("form").attr("action", actions[this.id]);
});
$('#button1').click(function(){
$('#myform').prop('action', 'page1.php');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With