Coder Social home page Coder Social logo

hongymagic / jquery.serializeobject Goto Github PK

View Code? Open in Web Editor NEW
149.0 9.0 78.0 26 KB

Encode a set of form elements as a JSON object for manipulation/submission.

Home Page: http://jsfiddle.net/davidhong/PRpJT/

License: MIT License

JavaScript 54.86% HTML 45.14%
javascript jquery jquery-plugin

jquery.serializeobject's Introduction

Build Status

What is it?

$.serializeObject is a variant of existing $.serialize method which, instead of encoding form elements to string, converts form elements to a valid JSON object which can be used in your JavaScript application.

Why?

Whilst it isn't necessary in most cases, and by that I mean 99.99% kind of most, there are times when we manipulate form data on client side. Personally I find JSON much easier to work with than DOM or string manipulation.

How do I use it?

If you want to see the code and demo first: http://jsfiddle.net/davidhong/PRpJT/

Simply include the jQuery.serializeObject.js along with any jQuery instance and use it like $.serialize.

If you have a form like the following:

<form id="minutes">
	<input type="text" name="subject">
	<input type="text" name="minute-taker">
	<!-- ... -->
	<input type="checkbox" name="attendees" value="David" checked="checked">
	<input type="checkbox" name="attendees" value="Daniel" checked="checked">
	<input type="checkbox" name="attendees" value="Darwin" checked="checked">
</form>

and wish to convert them to a JSON object:

var minutes = $('form#minutes').serializeObject();

will return:

{
	"subject": "",
	"minute-taker": "",
	"attendees": [
		"David",
		"Daniel",
		"Darwin"
	]
}

Change log

2.0.3

  • Add MIT License

2.0.2

  • Add support for $.noConflict mode

2.0.0

Major version change: Camel casing of names have been removed. Please use version 1.0.4 if you require camel casing of names.

  • Remove $.data like camelCasing on names

1.0.4

  • Fix an issue (#2) where arrays longer than 2 resulted in incorrect values

Known issues

Bitdeli Badge

jquery.serializeobject's People

Contributors

bitdeli-chef avatar hongymagic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.serializeobject's Issues

Feature Request: Support nested objects

This is a feature request for supporting nested objects by doing the following:

<input name="name.first"><input name="name.last">

The above input would render to a JSON object that would look like {"name":{"first":"J","last":"Lo"}}.

The use case for this is supporting custom fields for applications that require customized forms. This allows nesting of any type of data without requiring a Javascript coder to do heavy manipulation either client-side or server-side. Would love to see this, and could possibly contribute myself if you explain what I need in order to set up my dev environment.

Publish npm package

I don't think its there, it would be much appreciated if it can be published.

Old trick doesn't work

Hello,

Be warned, the old trick where you have:

<input name="cgv" value="false" autocomplete="off" type="hidden">
<input name="cgv" value="true" autocomplete="off" type="checkbox">

doesn't work anymore, because it makes an array.

I find this solution:

formData = $form.serializeObject()
if ( typeof formData.cgv === 'undefined' ) formData.cgv = false

but maybe it could be good to have data-attr to handle that.

Json one single value to multidimension array

Hello,

Is there any other way to convert from one single value to array? for example, I have below single object:
1st:
Object { js_language="1", js_language_level="1"}

and this is two values object:
2st:
Object { js_language=[2], js_language_level=[2]}

what I wanted is convert 1st from :
Object { js_language="1", js_language_level="1"}

to

Object { js_language=[1], js_language_level=[1]}

Are there any way to achieve it with serializeObject?

Thanks in advanced

deserialize?

Anyone have nice tricks for deserializing an object back to a form (i.e. if you want to edit the object it later).

Array names

Inputs with array names like "foo[0]" are not converted to an array. Instead the literal string "foo[0]" is used as the property name.

No license

While this seems like a good library that does it's job, I cannot seem to find a license file for it. This makes it impossible to use the library in any public project.

Please consider adding a license to the project so that people can safely use your project (MIT or BSD preferred)

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.