├── .bowerrc ├── .gitignore ├── .gitmodules ├── .jshintrc ├── .travis.yml ├── Gruntfile.js ├── LICENSE.txt ├── README.markdown ├── bower.json ├── dist └── jquery.serializeObject.min.js ├── jquery.serializeObject.d.ts ├── jquery.serializeObject.js ├── package.json ├── serializeObject.jquery.json └── test ├── serialization-test.js └── tests.html /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "components" 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | node_modules/ 4 | components/ 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongymagic/jQuery.serializeObject/7dbebed927938ece9eee63b09486642ca3a5ca58/.gitmodules -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise" : true, 3 | "curly" : true, 4 | "eqeqeq" : false, 5 | "forin" : true, 6 | "immed" : true, 7 | "latedef" : true, 8 | "newcap" : true, 9 | "noarg" : true, 10 | "noempty" : true, 11 | "nonew" : true, 12 | "plusplus" : false, 13 | "regexp" : true, 14 | "undef" : true, 15 | "strict" : false, 16 | "trailing" : true, 17 | 18 | "asi" : false, 19 | "boss" : false, 20 | "debug" : false, 21 | "eqnull" : false, 22 | "es5" : false, 23 | "esnext" : false, 24 | "evil" : false, 25 | "expr" : false, 26 | "funcscope" : false, 27 | "globalstrict" : false, 28 | "iterator" : false, 29 | "lastsemic" : false, 30 | "laxbreak" : false, 31 | "laxcomma" : false, 32 | "loopfunc" : false, 33 | "multistr" : false, 34 | "onecase" : false, 35 | "proto" : false, 36 | "regexdash" : false, 37 | "scripturl" : false, 38 | "smarttabs" : false, 39 | "shadow" : false, 40 | "sub" : false, 41 | "supernew" : false, 42 | "validthis" : false, 43 | 44 | "browser" : true, 45 | "couch" : false, 46 | "devel" : true, 47 | "dojo" : false, 48 | "jquery" : true, 49 | "mootools" : false, 50 | "node" : true, 51 | "nonstandard" : false, 52 | "prototypejs" : false, 53 | "rhino" : false, 54 | "wsh" : false, 55 | 56 | "nomen" : false, 57 | "onevar" : false, 58 | "passfail" : false, 59 | "white" : false, 60 | 61 | "maxerr" : 100, 62 | "predef" : [ 63 | "test", 64 | "deepEqual" 65 | ], 66 | "indent" : 4 67 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.11 4 | before_script: 5 | - npm install -g grunt-cli bower 6 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function (grunt) { 3 | 4 | // Project configuration 5 | 6 | grunt.initConfig({ 7 | pkg: '', 8 | 9 | files: { 10 | all: ['jquery.serializeObject.js'], 11 | tests: ['test/**/*.js'] 12 | }, 13 | 14 | docs: { 15 | all: ['README.markdown'] 16 | }, 17 | 18 | uglify: { 19 | dist: { 20 | 'dist/jquery.serializeObject.min.js': '' 21 | } 22 | }, 23 | 24 | jshint: { 25 | options: '', 26 | all: '' 27 | }, 28 | 29 | qunit: { 30 | all: ['test/*.html'] 31 | } 32 | 33 | }); 34 | 35 | grunt.loadNpmTasks('grunt-contrib-jshint'); 36 | grunt.loadNpmTasks('grunt-contrib-uglify'); 37 | grunt.loadNpmTasks('grunt-contrib-qunit'); 38 | 39 | // Default grunt task 40 | 41 | grunt.registerTask('default', ['jshint', 'uglify', 'qunit']); 42 | 43 | }; -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 David Hong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/hongymagic/jQuery.serializeObject.png)](https://travis-ci.org/hongymagic/jQuery.serializeObject) 2 | 3 | # What is it? 4 | 5 | `$.serializeObject` is a variant of existing `$.serialize` method which, instead 6 | of encoding form elements to string, converts form elements to a valid JSON 7 | object which can be used in your JavaScript application. 8 | 9 | # Why? 10 | 11 | Whilst it isn't necessary in most cases, and by that I mean 99.99% kind of most, 12 | there are times when we manipulate form data on client side. Personally I find 13 | JSON much easier to work with than DOM or string manipulation. 14 | 15 | # How do I use it? 16 | 17 | If you want to see the code and demo first: http://jsfiddle.net/davidhong/PRpJT/ 18 | 19 | Simply include the `jQuery.serializeObject.js` along with any `jQuery` instance 20 | and use it like `$.serialize`. 21 | 22 | If you have a `form` like the following: 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 | 33 | and wish to convert them to a JSON object: 34 | 35 | var minutes = $('form#minutes').serializeObject(); 36 | 37 | will return: 38 | 39 | { 40 | "subject": "", 41 | "minute-taker": "", 42 | "attendees": [ 43 | "David", 44 | "Daniel", 45 | "Darwin" 46 | ] 47 | } 48 | 49 | ## Change log 50 | 51 | ### 2.0.3 52 | 53 | - Add MIT License 54 | 55 | ### 2.0.2 56 | 57 | - Add support for $.noConflict mode 58 | 59 | ### 2.0.0 60 | 61 | *Major version change: Camel casing of names have been removed. Please use 62 | version 1.0.4 if you require camel casing of names.* 63 | 64 | - Remove `$.data` like camelCasing on names 65 | 66 | ### 1.0.4 67 | 68 | - Fix an issue (#2) where arrays longer than 2 resulted in incorrect values 69 | 70 | ## Known issues 71 | 72 | - In rare cases, this won't work with old checkbox/hidden hack. See [this issue for more details](https://github.com/hongymagic/jQuery.serializeObject/issues/9). 73 | 74 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/hongymagic/jquery.serializeobject/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 75 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jQuery.serializeObject", 3 | "version": "2.0.3", 4 | "main": "jquery.serializeObject.js", 5 | "ignore": [ 6 | "**/.*", 7 | "node_modules", 8 | "components" 9 | ], 10 | "dependencies": { 11 | "jquery": "1.9.1" 12 | }, 13 | "devDependencies": { 14 | "qunit": "~1.14.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /dist/jquery.serializeObject.min.js: -------------------------------------------------------------------------------- 1 | $.fn.serializeObject=function(){"use strict";var a={},b=function(b,c){var d=a[c.name];"undefined"!=typeof d&&d!==null?$.isArray(d)?d.push(c.value):a[c.name]=[d,c.value]:a[c.name]=c.value};return $.each(this.serializeArray(),b),a}; -------------------------------------------------------------------------------- /jquery.serializeObject.d.ts: -------------------------------------------------------------------------------- 1 | 2 | interface JQuery { 3 | serializeObject(): { [index: string]: string|string[] } 4 | } 5 | -------------------------------------------------------------------------------- /jquery.serializeObject.js: -------------------------------------------------------------------------------- 1 | // 2 | // Use internal $.serializeArray to get list of form elements which is 3 | // consistent with $.serialize 4 | // 5 | // From version 2.0.0, $.serializeObject will stop converting [name] values 6 | // to camelCase format. This is *consistent* with other serialize methods: 7 | // 8 | // - $.serialize 9 | // - $.serializeArray 10 | // 11 | // If you require camel casing, you can either download version 1.0.4 or map 12 | // them yourself. 13 | // 14 | 15 | (function($){ 16 | $.fn.serializeObject = function () { 17 | "use strict"; 18 | 19 | var result = {}; 20 | var extend = function (i, element) { 21 | var node = result[element.name]; 22 | 23 | // If node with same name exists already, need to convert it to an array as it 24 | // is a multi-value field (i.e., checkboxes) 25 | 26 | if ('undefined' !== typeof node && node !== null) { 27 | if ($.isArray(node)) { 28 | node.push(element.value); 29 | } else { 30 | result[element.name] = [node, element.value]; 31 | } 32 | } else { 33 | result[element.name] = element.value; 34 | } 35 | }; 36 | 37 | $.each(this.serializeArray(), extend); 38 | return result; 39 | }; 40 | })(jQuery); 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jQuery.serializeObject", 3 | "version": "2.0.4", 4 | "description": "A jQuery plugin to turn form data into JSON", 5 | "main": "jquery.serializeObject.js", 6 | "types": "jquery.serializeObject.d.ts", 7 | "directories": { 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "pretest": "bower install", 12 | "test": "grunt" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git://github.com/hongymagic/jQuery.serializeObject.git" 17 | }, 18 | "keywords": [ 19 | "jquery", 20 | "serialize", 21 | "object", 22 | "json", 23 | "serializeJSON", 24 | "serializeObject" 25 | ], 26 | "author": "David G. Hong", 27 | "license": "MIT", 28 | "gitHead": "7789084425b353624fc95487024459f364d11df6", 29 | "readmeFilename": "README.markdown", 30 | "devDependencies": { 31 | "grunt": "~0.4.1", 32 | "grunt-contrib-uglify": "~0.2.0", 33 | "grunt-contrib-jshint": "~0.4.3", 34 | "grunt-contrib-qunit": "~0.2.1" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /serializeObject.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serializeObject", 3 | "version": "2.0.3", 4 | "title": "jQuery serializeObject", 5 | "author": { 6 | "name": "David G. Hong", 7 | "email": "davidhong.code@gmail.com", 8 | "url": "https://github.com/hongymagic" 9 | }, 10 | "licenses": [ 11 | { 12 | "type": "MIT", 13 | "url": "MIT-LICENSE.txt" 14 | } 15 | ], 16 | "dependencies": { 17 | "jquery": ">=1.4" 18 | }, 19 | "description": "Convert your form data to into JSON (JavaScript Object Notation) format, so you can manipulate them easily. See Github project page for information.", 20 | "tags": [ 21 | "form", 22 | "JSON", 23 | "serializeObject", 24 | "serialize", 25 | "serialise" 26 | ], 27 | "homepage": "https://github.com/hongymagic/jQuery.serializeObject", 28 | "bugs": "https://github.com/hongymagic/jQuery.serializeObject/issues", 29 | "maintainers": [ 30 | { 31 | "name": "David G. Hong", 32 | "url": "https://github.com/hongymagic" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /test/serialization-test.js: -------------------------------------------------------------------------------- 1 | 2 | module('serialization test'); 3 | 4 | test('simple form test', function () { 5 | var form = $('form#simple-form'); 6 | var data = form.serializeObject(); 7 | var expected = { 8 | name: 'John Apple', 9 | age: '21', 10 | email: 'john.apple@apple.com', 11 | password: '', 12 | 'legal-age': 'yes' 13 | }; 14 | 15 | deepEqual(data, expected, 'Key/value pairs should be identical'); 16 | }); 17 | 18 | test('multi value inputs', function () { 19 | var form = $('form#multi-value-form'); 20 | var data = form.serializeObject(); 21 | var expected = { 22 | food: ['Banana', 'Melon'], 23 | drink: [ 24 | 'Water', 25 | 'Milk', 26 | 'Beer', 27 | 'Cocktail' 28 | ] 29 | }; 30 | 31 | deepEqual(data, expected, 'Multiple values should be an array'); 32 | }); 33 | -------------------------------------------------------------------------------- /test/tests.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | $.serializeObject tests 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | 18 |

QUnit Test Suite - Step Addon

19 |

20 |
21 |

22 |
    23 | 24 |
    25 |
    26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
    34 | 35 |
    36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 |
    46 |
    47 | 48 | 49 | --------------------------------------------------------------------------------