« The Visual Design of Wordpress 2.7 | Home | RE: FlickrDown lets you archive all your Flickr photos »
jQuery.serializeAnything : Serialize anything (and not just forms!)
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: http://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!
Tags
About this entry

Oct 27 2008 / 5:39 pm


Best of Bram.us
Start getting used to scratching and smudging the iPhone with this Flash based iPhone Simulator
Automatically wrap/float/contour text around freeform images using CSS for usage in (X)HTML pages.
Pimp Your Firefox to be a web all-star
A look at the inc.common.php, spreading handy functions.
Photoshop Script inspired in the Chlomoscript to unobtrusively pimp photos you have taken.
Methods to adding file management options to TinyMCE (Part 1), Extending TinyFCK (Part 2) and PHP file uploads overview (Spinoff 1).









Comments
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
@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.