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?
To do the same using jQuery would be: $(function(){ $('IdOfTheForm'). submit(function(){ this. action = 'the dynamic action'; return true; }); });
Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the element) which has the type attribute set to button (i.e. type=”button” ).
The task is to create an HTML form dynamically with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use document. createElement() to create the new elements and use setAttribute() method to set the attributes of elements.
Untested, but this should at least get you on your way:
$('#myForm').submit(function (event)
{
var action = '';
// compute action here...
$(this).attr('action', action);
});
Use jQuery submit event:
$(document).ready(function() {
$('#yourFormId').submit(function() {
$(this).attr('action', 'dynamicallyBuildAction');
return false;
});
});
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;
});
});
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