Probocop
Probocop

Reputation: 10552

Jquery - Dynamically build form action on submit?

I'm trying to have the action of a HTML form built when the user clicks the submit button.

So the user fills in a form, clicks submit, then the action gets built then it actually gets submitted. The reason being is because the form has a load of options on it which will be passed to a script.

How would I go about doing this with Jquery?

Upvotes: 7

Views: 29996

Answers (3)

Guffa
Guffa

Reputation: 700372

The plain Javascript solution would have a function:

function changeAction() {
  this.action = 'the dynamic action';
  return true;
}

In the form you would set the onsubmit event:

<form ... onsubmit="return changeAction();">

To do the same using jQuery would be:

$(function(){
  $('IdOfTheForm').submit(function(){
    this.action = 'the dynamic action';
    return true;
  });
});

Upvotes: 1

Jarek
Jarek

Reputation: 5935

Use jQuery submit event:

$(document).ready(function() {
  $('#yourFormId').submit(function() {
    $(this).attr('action', 'dynamicallyBuildAction'); 
    return false;
  });
});

Upvotes: 4

Matt Ball
Matt Ball

Reputation: 359836

Untested, but this should at least get you on your way:

$('#myForm').submit(function (event)
{
    var action = '';
    // compute action here...
    $(this).attr('action', action);
});

Upvotes: 14

Related Questions