DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: jQuery

Encode a set of form elements as a string

Share

The serialize() method creates a text string in standard URL-encoded notation by serializing form values.

One or more form elements (input, radio, checkbox etc.) can be used as a serializing.

You can use this serialized values as a URL query string while making an AJAX request.

Syntax:

$(selector).serialize()

Below we have shown an example for form serialize:

HTML:

<form action-xhr="#" method="post">
First Name:<input type="text" name="fname" /> <br/>
Last Name:<input type="text" name="lname" /> <br/>
Quota:<input type="checkbox" name="quota" value="Yes"/><br/>
<input type="submit" name="submit" id="submit" />
</form>
<pre id="result"></pre>

JQUERY:

$(function() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serialize()));
return false;
});
});

Learn about JSON.stringify() for more details.

The JSON.stringify() method converts a JavaScript value to a JSON string i.e. optionally replacing the values if a replacer function is specified or optionally includes only the specified properties when a replacer array is specified.

VIEW:

The output will be:

"fname=john&lname=doe&quota=Yes"

Note: JSON.stringify converts a JavaScript value to a JavaScript Object Notation (JSON) string.

Read Also:  jQuery jQuery’s Data Method

Note that the JSON is not a completely strict subset of JavaScript with the two line terminators i.e.line separator and paragraph separator which is not needing to be escaped in JSON but needing to be escaped in JavaScript.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Xiaomi introduces the Redmi Note 11 series, which offers even more value for money

Xiaomi is one of the few companies that offer consumers devices that are packed with…

2 days ago

5 Reasons to use a professional mortgage broker

When you are looking to apply for a mortgage the one thing that most people…

3 days ago

5 grooming tips from recruiters what they want from candidates in an interview

Grooming for an interview is nothing like it is in the movies. Most people end…

3 days ago

4 Tips to hiring a finance broker

Finance brokers, also known as mortgage brokers, are very important professionals that are used during…

3 days ago

Common Household Plumbing Problems That Sydney Residents Endure

Despite being one of the most expensive real estate markets in the world, Australians still…

3 days ago

5 Stylish Interior Design Ideas for Small Bathrooms

When it comes to bathrooms, you have to do what you can with what you’ve…

3 days ago