· 7 years ago · Feb 19, 2018, 10:36 PM
1/*
2Serialize Subsection - jquery.subsection.js
3Copyright (c) 2008 Nathan Hammond
4Released under the MIT license.
5
6With thanks to Michael Geary, Diego Perini, John-David Dalton, John Resig, and Garrett Smith for vetting elements of this idea.
7Requires jQuery revision 5826 or later with the change to jQuery.fn.serializeArray() to query this.elements.
8Requires Ancestry plugin.
9*/
10
11jQuery.fn.serializeSubsection = function ( refresh ) {
12 refresh = refresh || false; // Flag to identify if we should recalculate the subelements.
13
14 // Set up the .elements array on all matched DOM elements.
15 this.map(function() {
16 // Short circuit.
17 if ( !refresh && this.elements ) return;
18
19 // Traverse upward until we find the form.
20 for ( var form = this; !jQuery.nodeName( form, "form" ); form = form.parentNode )
21 if ( !form ) return; // Better luck next time, this element isn't in a form.
22
23 // Identity crisis.
24 var self = this;
25
26 // Get the array for the form elements.
27 // Filter it to only have the descendants of the subsection.
28 // Store it as if it were the relevant HTMLCollection for the subsection element.
29 this.elements = jQuery(jQuery.makeArray(form.elements)).descendantOf(self).get();
30
31 });
32 return this.serializeArray();
33};