Reputation: 19138
How do I get the action url from a form with jquery?
Upvotes: 62
Views: 121027
Reputation: 19560
Get the form, ask for the action
attribute:
$('#myForm').attr('action');
Upvotes: 139
Reputation: 5405
Try this one:
var formAction = $('#form')[0].action;
Or on form sumit:
$("#form").submit(function (event) {
event.preventDefault();
var frmAction=this.action;
});
Upvotes: 4
Reputation: 3951
Generally it's better to use prop()
instead of attr()
when grabbing these sorts of values. (This prop() vs attr() stackoverflow post explains why.)
However, in this instance, @JAAulde answer is exactly what I needed, but not might be what you need. You might want the full action url.
<form action="handler.php" method="post" id="myForm">
$('#myForm').attr('action');
// returns 'handler.php'
$('#myForm').prop('action');
// returns 'http://www.example.com/handler.php'
Check out the cool ascii table in this stackoverflow post to learn more.
Upvotes: 38
Reputation: 640
To use the action attribute of a form use:
$( '#myForm' ).attr( 'action' );
like @JAAulde said.
To use an entered value from a form use:
$('#myInput').val();
Upvotes: 3