Skip to content

Instantly share code, notes, and snippets.

@mkmms
Created August 29, 2019 11:08
Show Gist options
  • Save mkmms/aa9621408672ef1715d03615a3450c33 to your computer and use it in GitHub Desktop.
Save mkmms/aa9621408672ef1715d03615a3450c33 to your computer and use it in GitHub Desktop.
jQuery get params from any parent element
$.fn.generateParams = function() {
var rsubmittable = /^(?:input|select|textarea|keygen)/i;
var rcheckableType = ( /^(?:checkbox|radio)$/i );
var rCRLF = /\r?\n/g;
return this
.find("input, select, textarea, keygen")
.not(":disabled")
.filter(function(){
var type = this.type;
return this.name && !jQuery( this ).is( ":disabled" ) &&
rsubmittable.test( this.nodeName ) &&
( this.checked || !rcheckableType.test( type ) );
})
.map( function( i, elem ) {
var val = jQuery( this ).val();
return val == null ?
null :
jQuery.isArray( val ) ?
jQuery.map( val, function( val ) {
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
} ) :
{ name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
} ).get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment