How to know which form I clicked with the button class

advertisements

How can i know wich form i clicked? It can be possible with button class instead of buttons with id?

$(document).ready(function () {
    $(".form-buttons").click(function () {
        //I only want the form wich corresponds to the button i clicked
        var formDates = $(form).serialize()
        alert ("You clicked "+formDates)
    })
})

<form id="form1">
    <input type="text" value="date1" name="name1"/>
    <input type="text" value="date2" name="name2"/>
    <input type="text" value="date3" name="name3"/>
    <button type="button" class="form-button"></button>
</form>
<form id="form2">
    <input type="text" value="date4" name="name1"/>
    <input type="text" value="date5" name="name2"/>
    <input type="text" value="date6" name="name3"/>
    <button type="button" class="form-button"></button>
</form>


Yes use class instead of id for similar elements. Please try this.

Note: form-button is the class name in your HTML and not form-buttons

$(document).ready(function () {
    $(".form-button").click(function () {
        var formDates = $(this).closest('form').serialize();
        alert ("You clicked "+formDates)
    })
})