jQuery serializeArray() with Examples
The serializeArray() is an inbuilt method in jQuery that is used to create a JavaScript array of objects that is ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls. The controls can be of several types. JSON string is a text and can convert any JavaScript object into JSON, and send JSON to the server.
Syntax:
$(selector).serializeArray()
Parameter: It does not accept any parameter.
Return Value: It return a string of objects.
jQuery code to show the working of serializeArray() method:
Example: In this example, we are using above-explained method.
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function () {
$("button").click(function () {
let x = $("form").serializeArray();
$.each(x, function (i, field) {
$("#d").append(field.name + ":" + field.value + ":::");
});
});
});
</ script >
< style >
#d1 {
width: 300px;
height: 100px;
padding: 20px;
border: 2px solid green;
margin-bottom: 10px;
}
</ style >
</ head >
< body >
< div id = "d1" >
< form action = "" >
Site name:
< input type = "text"
name = "SiteName"
value = "GeeksforGeeks" >
< br >
< br > Contributor name:
< input type = "text"
name = "ContributorName"
value = "KundanJha" >
< br >
</ form >
< button >Click here!</ button >
</ div >
< div id = "d" ></ div >
</ body >
</ html >
|
Output:
Last Updated :
12 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...