Last active
September 10, 2015 20:17
-
-
Save johnmurch/8a114ce9da4c34caaa40 to your computer and use it in GitHub Desktop.
Extract JS Object from Form
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.extractObject = function() { | |
var accum = {}; | |
function add(accum, namev, value) { | |
if (namev.length == 1) | |
accum[namev[0]] = value; | |
else { | |
if (accum[namev[0]] == null) | |
accum[namev[0]] = {}; | |
add(accum[namev[0]], namev.slice(1), value); | |
} | |
}; | |
this.find('input, textarea, select').each(function() { | |
add(accum, $(this).attr('name').split('.'), $(this).val()); | |
}); | |
return accum; | |
} | |
// ... | |
var object = $('#testformId').extractObject(); | |
console.log(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
<form id = "testform"> | |
<input type="text" name="name[]" id="p" /> | |
<input type="text" name="manuf[]" id="k" size="3" /> | |
<input type="text" name="item_pr[]" id="ka" size="10" /> | |
<input type="text" name="price" id="su" size="10" disabled="disabled"/><br /> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment