Use the same script for multiple forms

Asked

Viewed 72 times

1

How do I use only one script for multiple forms?

  <script>
    $(document).ready(function() {
        $("#formPost").submit(function() {
            var dados = $(this).serialize();

            $.ajax({
                type: 'POST',
                url: 'https://jsonplaceholder.typicode.com/posts',
                data: dados,
                success: function(data) {

                }
            });
            return false;
        });
    });
</script>

<form action="" method="POST" id="formPost">
    <input type="text" name="title" />
    <input type="text" name="body" />
    <input type="submit" value="Enviar" />
</form>

Thanks in advance!

1 answer

2


You can invoke the form for tag, rather than rely on id, example:

$("form").submit(function() {
...

In this case, the same script can be copied (or imported) to multiple pages with different id forms.

If there are more than one form per page, all of them will also be affected, and will have the same "Submit Function".

  • Thanks brother! It worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.