serialize() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:serialize

Description

The serialize() method encodes form value to create URL string.

The following code shows how to Output the result of serialized form values:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var x = $("form").serialize();
        $.each(x, function(i, field){
            $("#results").append(field.name + ":" + field.value + " ");
        });//  w ww .jav  a 2  s .  com
    });
});
</script>
</head>
<body>

<form action="">
  First name: <input type="text" name="FirstName" value="Bond"><br>
  Last name: <input type="text" name="LastName" value="Mary"><br>
</form>

<button>Serialize form values</button>

<div id="results"></div>

</body>
</html>

Related Tutorials