Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
Created February 18, 2011 02:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kara-ryli/833149 to your computer and use it in GitHub Desktop.
Save kara-ryli/833149 to your computer and use it in GitHub Desktop.
Serialize a form into a query string with YUI3
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);
});
@Burn0ut07
Copy link

Thanks this is just what I needed!

@colonelmac
Copy link

Thanks man!

@timvlaer
Copy link

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