Code Monkey home page Code Monkey logo

serializeobject's Introduction

serializeObject

This jquery plugin will map the serialized form data array to JSON Object.

Dependency
  1. jquery library.
To Get Started
  1. import the jquery library.
  2. import the jquery.serializeObject.js plugin.
First Usage
$( "form" ).serializeObject();
Second Usage
var rFormat = {
	id: 0,
	name: "John Doe"
};

$( "form" ).serializeObject( { requiredFormat: rFormat } );
Example 01
<form>
	<input type="text" name="txt01" value="Text 01" />
	<input type="text" name="txt02" value="Text 02" />

	<input type="submit" value="Submit" />
</form>

<script>
	( function( $ ){
		$( document ).ready( function(){
			$( "form" ).submit( function( e ) {
				e.preventDefault();

				var jsonObject = $( this ).serializeObject();
				console.log( jsonObject );
			} );
		} );
	} )( jQuery );
</script>
Example 01: Console Output
Object {
	txt01: "Text 01",
	txt02: "Text 02"
}
Example 02
<form>
	<input type="text" name="txt01[]" value="Text 01" />
	<input type="text" name="txt01[]" value="Text 02" />

	<input type="submit" value="Submit" />
</form>

<script>
	( function( $ ){
		$( document).ready( function(){
			$( "form" ).submit( function( e ) {
				e.preventDefault();

				var jsonObject = $( this ).serializeObject();
				console.log( jsonObject );
			} );
		} );
	} )( jQuery );
</script>
Example 02: Console Output
Object {
	txt01: Array(2) {
		0: "Text 01",
		1: "Text 02"
	}
}
Example 03
<form>
	<input type="text" name="txt01[name][]" value="Text 01" />
	<input type="text" name="txt01[name][]" value="Text 02" />

	<input type="text" name="txt01[phone][]" value="000001" />
	<input type="text" name="txt01[phone][]" value="000002" />

	<input type="submit" value="Submit" />
</form>

<script>
	( function( $ ){
		$( document ).ready( function(){
			$( "form" ).submit( function( e ) {
				e.preventDefault();

				var jsonObject = $( this ).serializeObject();
				console.log( jsonObject );
			} );
		} );
	} )( jQuery );
</script>
Example 03: Console Output
Object 
{
	txt01: Object 
	{
		name: Array()
		{
			0: Text 01
			1: Text 02
		},
		
		phone: Array()
		{
			0: 000001
			1: 000002
		}
	}
}
Example 04
<form>
	<input type="text" name="txt01[][name]" value="Text 01" />
	<input type="text" name="txt01[][phone]" value="000001" />

	<input type="text" name="txt01[][name]" value="Text 02" />
	<input type="text" name="txt01[][phone]" value="000002" />

	<input type="submit" value="Submit" />
</form>

<script>
	( function( $){
		$( document ).ready( function(){
			$( "form" ).submit( function( e ) {
				e.preventDefault();

				var jsonObject = $( this ).serializeObject();
				console.log( jsonObject );
			} );
		} );
	} )( jQuery );
</script>
Example 04: Console Output
Object 
{
	txt01: Array(2) 
	{
		0: Object
		{
			name: Text 01
			phone: 000001
		},
		
		1: Object
		{
			name: Text 02
			phone: 000002
		}
	}
}
Example 05
<form>
	<input type="text" name="txt01[][name]" value="Text 01" />
	<input type="text" name="txt01[][phone]" value="000001" />

	<input type="text" name="txt01[][name]" value="Text 02" />
	<input type="text" name="txt01[][phone]" value="000002" />

	<input type="submit" value="Submit" />
</form>

<script>
	(function( $ ){
		$( document ).ready( function(){
			$( "form" ).submit( function( e ) {
				e.preventDefault();

				var reqFormat = {
					id: 0,
					company: "ACN"
				}; 

				var jsonObject = $( this ).serializeObject( { requiredFormat: reqFormat } );
				console.log(jsonObject);
			} );
		} );
	})( jQuery );
</script>
Example 05: Console Output
Object 
{
	id: 0,
	company: "ACN",
	txt01: Array(2) 
	{
		0: Object
		{
			name: Text 01
			phone: 000001
		},
		
		1: Object
		{
			name: Text 02
			phone: 000002
		}
	}
}

serializeobject's People

Contributors

citnvillareal avatar

Watchers

James Cloos avatar 一灯大师 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.