Get a form action/url with jquery

JqueryFormsasp.net Mvc-2

Jquery Problem Overview


How do I get the action url from a form with jquery?

Jquery Solutions


Solution 1 - Jquery

Get the form, ask for the action attribute:

$('#myForm').attr('action');

Solution 2 - Jquery

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.

Solution 3 - Jquery

Try this one:

var formAction = $('#form')[0].action;

Or on form sumit:

 $("#form").submit(function (event) {
    event.preventDefault();
    var frmAction=this.action;
});

Solution 4 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDejan.SView Question on Stackoverflow
Solution 1 - JqueryJAAuldeView Answer on Stackoverflow
Solution 2 - JqueryelbowlobstercowstandView Answer on Stackoverflow
Solution 3 - JqueryFereydoon BarikzehyView Answer on Stackoverflow
Solution 4 - JqueryOwenView Answer on Stackoverflow