(function ($) { $.fn.serializeFormJSON = function () { var o = {}; var a = this.serializeArray(); $.each(a, function () { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; })(jQuery); $('form').submit(function (e) { e.preventDefault(); var data = $(this).serializeFormJSON(); console.log(data); /* Object email: "value" name: "value" password: "value" */});
or another approach
function jQFormSerializeArrToJson(formid) { var serializedArr = $("form#" + formid).serializeArray(); var jsonObj = {}; jQuery.map(formSerializeArr, function(n, i) { jsonObj[n.name] = n.value; }); return jsonObj; } $('#formidlogin').submit(function(e) { e.preventDefault(); var data = jQFormSerializeArrToJson('formidlogin'); data = JSON.stringify(data), console.log(data); /* Object email: "value" name: "value" password: "value" */});
To collect a group of elements, we pass the selector to the jQuery function using…
jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…
HTML:- [crayon-67a5ae302e052828522911/] CSS:- [crayon-67a5ae302e055537354412/] JavaScript:- [crayon-67a5ae302e056163332454/]
Prevent users typing special characters in text box, textarea, etc. [crayon-67a5ae302e119275433980/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…
Any interactive web site has form input - a place where the users input different…
[crayon-67a5ae302e30a277095114/]