Is there a way to use jQuery's serialize form fields and trim the value in the fields?
forms, jquery, serialization, trim
Solution
You could try looping through the object and triming everything.
//Serialize form as array
var serializedForm = form.serializeArray();
//trim values
for(var i =0, len = serializedForm.length;i<len;i++){
serializedForm[i] = $.trim(serializedForm[i]);
}
//turn it into a string if you wish
serializedForm = $.param(serializedForm);
Problem
I have a form that uses jQuery to submit an ajax post and it serializes the form that is sent up. The code looks like this: ``` var form = $("form"); var action = form.attr("action"); var serializedForm = form.serialize(); $.post(action, serializedForm, function(data) { ... }); ``` The problem here is that if a field has trailing white space, the serialize function will turn those spaces to plus (+) signs, when they should be stripped. Is there a way to get the fields trimmed without doing the following: ``` $("#name").val( jQuery.trim( $("#name") ) ); ```