How do I get the action url from a form with jquery?
Asked
Active
Viewed 1.1e+01k times
4 Answers
132
Get the form, ask for the action
attribute:
$('#myForm').attr('action');

Rikin Patel
- 8,848
- 7
- 70
- 78

JAAulde
- 19,250
- 5
- 52
- 63
-
2Great answer. I was curious if `attr()` was the best way, as opposed to the newer `prop()`. Turns out [they both return something different](http://stackoverflow.com/a/34212885/3965565). – elbowlobstercowstand Dec 10 '15 at 22:30
35
Need the full action url instead of just the basename?
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.
Consider this html form start tag:
<form action="handler.php" method="post" id="myForm">
attr() returns the exact action value:
$('#myForm').attr('action');
// returns 'handler.php'
prop() returns the full action url:
$('#myForm').prop('action');
// returns 'http://www.example.com/handler.php'
Check out the cool ascii table in this stackoverflow post to learn more.

Community
- 1
- 1

elbowlobstercowstand
- 3,812
- 1
- 25
- 22
4
Try this one:
var formAction = $('#form')[0].action;
Or on form sumit:
$("#form").submit(function (event) {
event.preventDefault();
var frmAction=this.action;
});

fbarikzehy
- 4,885
- 2
- 33
- 39
3
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();

Owen
- 630
- 6
- 21
-
1Forms themselves don't have values, the form elements contained within them do. Your comment seems a little confused. – Rory McCrossan Apr 11 '11 at 11:26
-
Yes, #myInput refers to an input element within the form, where a URL might be entered. – Owen May 16 '11 at 07:13
-
1Ok, if that's the case, the `#myInput` is likely not to have an `action` attr. @RoryMcCrossan's point is that it won't have both – Landon Mar 30 '16 at 22:55