How To Use SerializeArray Method In JQuery With Example

admin_img Posted By Bajarangi soft , Posted On 16-10-2020

In JQuery The serializeArray() used to create a JavaScript array of objects that is ready to be encoded as a JSON string.So today in this article we discuss about serializeArray method with jquery

How To Use SerializeArray Method In JQuery With Example

Step 1:Create index.html file and implement below code.

<form action="">
    Site name:
    <input type="text" name="SiteName" value="BajarangiSoft">
    <br>
    <br> Contributor name:
    <input type="text" name="ContributorName" value="Bsoft">
    <br>
</form>

Step 2:Implement jQuery to use serializeArray() method.
<script>
    $(document).ready(function() {
        $("button").click(function() {
            var x = $("form").serializeArray();
            $.each(x, function(i, field) {
                $("#d").append(field.name + ":" + field.value + ":::");
            });
        });
    });
</script>

Complete Code For Using SerializeArray Method In JQuery
<!DOCTYPE html>
<html>
<head>
    <title>How To Use SerializeArray Method In JQuery With Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: black;
    }

</style>
<body>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h1 id="color" style="color: White;">Use SerializeArray Method In JQuery </h1>
    </div>
    <br>
    <div class="col-md-2"></div>
    <div class="col-md-8">
        <div class="well">
            <form action="">
                Site name:
                <input type="text" name="SiteName" value="BajarangiSoft">
                <br>
                <br> Contributor name:
                <input type="text" name="ContributorName" value="Bsoft">
                <br>
            </form>

            <!-- click on this button -->
            <button class="btn btn-success">Click here!</button>
            <div id="d"></div>
            <script>
                $(document).ready(function() {
                    $("button").click(function() {
                        var x = $("form").serializeArray();
                        $.each(x, function(i, field) {
                            $("#d").append(field.name + ":" + field.value + ":::");
                        });
                    });
                });
            </script>
        </div>
    </div>
    <div class="col-md-2"></div>
</div>
</body>
</html>

Related Post