Posted By

virgil on 10/14/10


Tagged

form data object json serialize


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

mloberg
kubaitis
Tyster
rygiel


Serialize Form to JSON


 / Published in: jQuery
 

URL: http://tobiascohen.com/files/stackoverflow/jquery-form-serializeObject.html

usage; $.toJSON($(\'form\').serializeObject());

  1. $.fn.serializeObject = function()
  2. { //Prepping for JSON
  3. var o = {};
  4. var a = this.serializeArray();
  5. $.each(a, function() {
  6. if (o[this.name]) {
  7. if (!o[this.name].push) {
  8. o[this.name] = [o[this.name]];
  9. }
  10. o[this.name].push(this.value || '');
  11. } else {
  12. o[this.name] = this.value || '';
  13. }
  14. });
  15. return o;
  16. };

Report this snippet  

You need to login to post a comment.