Created
August 13, 2008 15:27
-
-
Save nathanhammond/5242 to your computer and use it in GitHub Desktop.
Extension to jQuery to imitate the Form.elements array for any arbitrary descendant of a form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Serialize Subsection - jquery.subsection.js | |
Copyright (c) 2008 Nathan Hammond | |
Released under the MIT license. | |
With thanks to Michael Geary, Diego Perini, John-David Dalton, John Resig, and Garrett Smith for vetting elements of this idea. | |
Requires jQuery revision 5826 or later with the change to jQuery.fn.serializeArray() to query this.elements. | |
Requires Ancestry plugin. | |
*/ | |
jQuery.fn.serializeSubsection = function ( refresh ) { | |
refresh = refresh || false; // Flag to identify if we should recalculate the subelements. | |
// Set up the .elements array on all matched DOM elements. | |
this.map(function() { | |
// Short circuit. | |
if ( !refresh && this.elements ) return; | |
// Traverse upward until we find the form. | |
for ( var form = this; !jQuery.nodeName( form, "form" ); form = form.parentNode ) | |
if ( !form ) return; // Better luck next time, this element isn't in a form. | |
// Identity crisis. | |
var self = this; | |
// Get the array for the form elements. | |
// Filter it to only have the descendants of the subsection. | |
// Store it as if it were the relevant HTMLCollection for the subsection element. | |
this.elements = jQuery(jQuery.makeArray(form.elements)).descendantOf(self).get(); | |
}); | |
return this.serializeArray(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment