Sumit Jha
Sumit Jha

Reputation: 2195

Selecting element in jQuery

I have many forms in my webpage with the same class-name.

<form method="post" action="" class="like-form">
    <input type="hidden" value="10"/>
    <!--some other input tags -->
    <input type="submit" value="like" class="btn btn-primary like"/>
</form>

What I want to do is: Whenever I hit a like button, I want to get the data of the form (containing this 'like' button) by doing something like:

$(".like").click(function(event) {      
    event.preventDefault();
    var formData = $(this).prev(".like-form").serialize();
    console.log(formData);
});

But this doesn't work. How can I achieve this in jQuery?

Upvotes: 0

Views: 29

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

Use closest() instead of prev().

var formData = $(this).closest(".like-form").serialize();

Upvotes: 4

Related Questions