7

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?

2
  • I'll caution you to think about what happens when your user has JS disabled. The form would still get submitted, but without the form's action getting changed. Does this break your server-side script? Commented Apr 20, 2010 at 18:21
  • 1
    Hi, normally I would not rely on JS for core functionality, but this is just for an in house tool I'm building so it's not so much of a problem. Commented Apr 21, 2010 at 8:24

3 Answers 3

14

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

$('#myForm').submit(function (event)
{
    var action = '';
    // compute action here...
    $(this).attr('action', action);
});
Sign up to request clarification or add additional context in comments.

Comments

4

Use jQuery submit event:

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

Comments

1

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;
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.