Created
November 20, 2016 20:20
-
-
Save dustinpoissant/3f171ad879f1abf3e035f3cc6aa3d814 to your computer and use it in GitHub Desktop.
A jQuery plugin that gets the forms data as an Object.
This file contains hidden or 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
$.fn.hasAttr=function(a){var b=$(this).attr(a);return void 0!==typeof b&&b!==!1}; | |
$.fn.getFormData = function(){ | |
var data = {}; | |
$(this).find("input, select, textarea").each(function(i, input){ | |
var $input = $(input); | |
if($input.hasAttr("name")){ | |
if($input.attr("type") && $input.hasAttr("type") && $input.attr("type").toLowerCase() == "checkbox"){ | |
if($input.is(":checked")){ | |
data[$input.attr("name")] = true; | |
} else { | |
data[$input.attr("name")] = false; | |
} | |
} else if( | |
$input.attr("type") && | |
$input.hasAttr("type") && | |
$input.attr("type").toLowerCase() == "radio" | |
){ | |
if($input.is(":checked")){ | |
data[$input.attr("name")] = $input.val(); | |
} else if(data[$input.attr("name")] == undefined){ | |
data[$input.attr("name")] = null; | |
} | |
} else { | |
data[$input.attr("name")] = $input.val(); | |
} | |
} | |
}); | |
return data; | |
}; |
This file contains hidden or 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
$.fn.hasAttr=function(a){var b=$(this).attr(a);return void 0!==typeof b&&b!==!1},$.fn.getFormData=function(){var a={};return $(this).find("input, select, textarea").each(function(b,c){var d=$(c);d.hasAttr("name")&&(d.attr("type")&&d.hasAttr("type")&&"checkbox"==d.attr("type").toLowerCase()?d.is(":checked")?a[d.attr("name")]=!0:a[d.attr("name")]=!1:d.attr("type")&&d.hasAttr("type")&&"radio"==d.attr("type").toLowerCase()?d.is(":checked")?a[d.attr("name")]=d.val():void 0==a[d.attr("name")]&&(a[d.attr("name")]=null):a[d.attr("name")]=d.val())}),a}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment