Elisabeth
Elisabeth

Reputation: 21206

Get the form of the clicked link with jquery

How can I get the form object containg the button which I clicked? I do not want to give the form an id.

$('#buttonid@(Model.Id)').on('click', function (e) {
 e.preventDefault();

 // get form here which is around the link button I clicked?              

}

Upvotes: 1

Views: 25

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337580

You can use closest() to find a parent element matching a given selector:

$('#buttonid@(Model.Id)').on('click', function (e) {
    e.preventDefault();
    var $form = $(this).closest('form');
    // do something with $form...
});

Upvotes: 4

Related Questions