Created
February 18, 2011 02:26
-
-
Save kara-ryli/833149 to your computer and use it in GitHub Desktop.
Serialize a form into a query string with YUI3
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
YUI().use('node', 'array-extras', 'querystring-stringify', function (Y) { | |
var form = Y.one('FORM_SELECTOR'), query; | |
query = Y.QueryString.stringify(Y.Array.reduce(Y.one(form).all('input[name],select[name],textarea[name]')._nodes, {}, function (init, el, index, array) { | |
init[el.name] = el.value; | |
return init; | |
})); | |
console.log(query); | |
}); |
Thanks man!
Nice!
Because I have checkboxes and radiobuttons in my form, I adapted the reduce function a bit to get the selected value:
function (init, el, index, array) {
var isCheckable = (el.type == "checkbox" || el.type == "radio");
if ((isCheckable && el.checked) || !isCheckable) {
init[el.name] = el.value;
}
return init;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks this is just what I needed!