83

I'm trying to access the post target action in a jquery function.

example:

<form action="/page/users" id="signup" method="post">

I'd like to access the "action" part - "/page/users" in this case.

$('#signup').live("submit", function(event) {
    // get this submitted action
}

Seems like I'm missing something very simple. I see the value in the dom but don't know where it's stored in jquery.

Thanks!

3 Answers 3

131
$('#signup').on("submit", function(event) {
    $form = $(this); //wrap this in jQuery

    alert('the action is: ' + $form.attr('action'));
});
Sign up to request clarification or add additional context in comments.

5 Comments

isn't the second line unnecessary, couldnt the third line be like alert('the action is: ' + $(this).attr('action'));
True @9kSoft, but we get the idea :)
FYI: live() was deprecated in 1.7 and removed in 1.9. Devs should now use on() instead.
This doesn't appear to work when a form has multiple submit buttons with different actions.
Sorry, but this will not work if you will be using formaction attribute w3schools.com/tags/att_button_formaction.asp
18

Try this ocde;;

var formAction = $("#signup").attr('action');

Comments

11

Clean and Simple:

$('#signup').submit(function(event) {

      alert(this.action);
});

2 Comments

Don't rely on this attribute since it may return the list of subnodes having name action.
First,the only use of action attribute is inside a html form tag.Second, nested forms are not best practice or good idea to have an clean optimized Html DOM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.