jQuery: Find a Form Action Using Jquery

This is pretty useful for people trying to pass in a generated action to a javascript file.  Eh?

SKIP IF YOU DON’T CARE ABOUT A REASON FOR USING THIS:

Say you are using the jQuery post method to send things back to the server but all the code for that is in a seperate javascript file.  Well you can’t really do this:

jQuery.ajax({
      type:'POST',
      url: <%= someMethodForCreatingAUrl('controller', 'action') %>,
      dataType:'json',
      data:{
        email: user.userName,
        password: user.password
      },
      success: function(result){
        onSuccess(result);
      },
      error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
      }
    });

if that is in the javascript file.

What you can do is this on the html file:

  <form id="formCreateUser" name="formCreateUser" method="post" action="${someMethodForCreatingAUrl('controller', 'action')}">

And in the javascript file:

ANSWER:

  var formAction = jQuery(ELEMENT_LOGIN_FORM).attr('action');

And there you go. You have the action.