Skip to content

Instantly share code, notes, and snippets.

@UziTech
Created August 6, 2014 19:29
Show Gist options
  • Save UziTech/3b926e732e3a6391b2ab to your computer and use it in GitHub Desktop.
Save UziTech/3b926e732e3a6391b2ab to your computer and use it in GitHub Desktop.
Trim all strings in an object
/**
* DWTFYW License
*
* Author: Tony Brix, http://tonybrix.info
*
* This function trims all strings in an object or array
*
* This is useful when sending ajax requests
*
* var vals = {
* this: $("#this").val(),
* that: $("#that").val(),
* the: {
* other: $("#other").val(),
* thing: $("#thing").val()
* }
* }
*
* trimAll(vals);
*
* $.post("someurl.php", vals, function(data){
* alert("do something");
* }, "json");
*
*/
function trimAll(obj) {
for (var prop in obj) {
if (typeof obj[prop] === "string") {
obj[prop] = obj[prop].trim();
} else {
trimAll(obj[prop]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment