0

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?

1 Answer 1

4

Use closest() instead of prev().

var formData = $(this).closest(".like-form").serialize();
Sign up to request clarification or add additional context in comments.

Comments

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.