Jquery to change form action

Jquery

Jquery Problem Overview


I have two buttons in a form and two different pages have to be called when they are clicked. when button1 is clicked then page1 must be loaded and when button2 is clicked then page2 must be loaded. i know how to do this in javascript but i have no clue about how to do this in jquery.Can any one help me?

Jquery Solutions


Solution 1 - Jquery

Try this:

$('#button1').click(function(){
   $('#formId').attr('action', 'page1');
});


$('#button2').click(function(){
   $('#formId').attr('action', 'page2');
});

Solution 2 - Jquery

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.

Solution 3 - Jquery

Use jQuery.attr() in your click handler:

$("#myform").attr('action', 'page1.php');

Solution 4 - Jquery

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". > >

> >
>
>

Solution 5 - Jquery

For variety's sake:

var actions = {input1: "action1.php", input2: "action2.php"};
$("#input1, #input2").click(function() {
    $(this).closest("form").attr("action", actions[this.id]);
});

Solution 6 - Jquery

$('#button1').click(function(){
$('#myform').prop('action', 'page1.php');
});

Solution 7 - Jquery

To change action value of form dynamically, you can try below code: > below code is if you are opening some dailog box and inside that dailog box you have form and you want to change the action of it. I used Bootstrap dailog box and on opening of that dailog box I am assigning action value to the form.

$('#your-dailog-id').on('show.bs.modal', function (event) {
    var link = $(event.relatedTarget);// Link that triggered the modal
    var cURL= link.data('url');// Extract info from data-* attributes
    $("#delUserform").attr("action", cURL);
});

> If you are trying to change the form action on regular page, use below code

$("#yourElementId").change(function() { 
  var action = <generate_action>;
  $("#formId").attr("action", action);
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnikhilView Question on Stackoverflow
Solution 1 - JqueryemcoView Answer on Stackoverflow
Solution 2 - JqueryEdwin V.View Answer on Stackoverflow
Solution 3 - JqueryJesse BunchView Answer on Stackoverflow
Solution 4 - JqueryFernando VieiraView Answer on Stackoverflow
Solution 5 - Jquerykarim79View Answer on Stackoverflow
Solution 6 - JqueryJoão MelloView Answer on Stackoverflow
Solution 7 - JqueryGampeshView Answer on Stackoverflow