Code snippet I found somewhere in my archive: jQuery.serializeAnything()
is a jQuery Extension that serializes any element you specify, in contrast to jQuery’s builtin serialize()
function which is limited to serializing form
elements only.
/* @projectDescription jQuery Serialize Anything - Serialize anything (and not just forms!)
* @author Bramus! (Bram Van Damme)
* @version 1.0
* @website: https://www.bram.us/
* @license : BSD
*/
(function($) {
$.fn.serializeAnything = function() {
var toReturn = [];
var els = $(this).find(':input').get();
$.each(els, function() {
if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
var val = $(this).val();
toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
}
});
return toReturn.join("&").replace(/%20/g, "+");
}
})(jQuery);
Usage is simple: place the contents above in a .js file (jquery.serializeanything.js
for example), refer to that file from your head element AFTER you referenced the jquery.js
file, and use it just as you would call the serialize()
function in your code: $('#myElement').serializeAnything()
.
Hf!
Consider donating.
I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!
Hi, I just came across this. Simple, convenient, and just what I needed. Thanks!
Excelent!!!
I tried $(‘#myElement’).serialize() and it did the same thing 🙂
serialize() only work in a wrapper, and this plugin will work without
@Florin: pre jQuery 1.3 that didn’t work 😉
That’s really works! I used jQuery 1.3. Thanks!
Omg this is what I am looking for, thank you a lot!
Very usefull indeed!
Does not work with a multiple select box or file inputs.
let jQuery do the stuff for you…
$(‘#ingreso_detalle tbody tr:first td:first’)
.find(‘[name]:input’) //since version 1.0
.clone() //since version 1.0
.wrap(”) //since version 1.0
.serialize() //since version 1.0
Just what I was looking for, thanks.
Hi. I just want to say thanks. This is nice.
Great!! exactly I need for serialize disable elements of my form
Thanks for this, helped to serialize data our ajaxy site.
I’v used your code to create a new plugin: tadada [b]serializeall[/b] – to json or to uri.
I found the json conversion code on jquery serializeArray page.
code: http://pastie.org/4411132
Can it serialize images
Thanks for the snippet! But please use normal quotes in your code I spend an hour searching for the error that came up every time 🙁
Awesome!
Hi,
this function will serialize first time but if i move back to form n update a few values it wont show the result as new values but same old values.