Tuesday, November 13, 2012

javascript – jQuery Serialize form into JSON object but ignore placeholder

Ok A while back I found this wonderful little addon style script for jQuery that will take all fields in a form and serialize them into a JSON object. Unfortunately however. I am finding that this function will use the placeholder text if none is otherwise provided. Which if its the case the the placeholder text is the text there I would rather it be set to null.


Any ideas how I can add that into this little snip?



(function($) {
$.fn.serializeFormJSON = function() {

var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || \'\');
} else {
o[this.name] = this.value || \'\';
}
});
return o;
};
})(jQuery);


edit:

In other words the placeholder attribute could be something like placeholder=\"First name\" and it seems javascript is treating that as an actual value when grabbing the inputs. So Im getting entries for a first name field (optional) as \”First name\”. So what I\’d like is to figure out how to compare the current field the above snip is on, and have it see the current placeholder for that field (if any as things like selects dont have that attribute). And if the value being pulled is a match for the placeholder attribute I want to add that field to the object being created but as null instead of the placeholder text






Rating: 1 out of 5 based on 3 ratings



The post javascript – jQuery Serialize form into JSON object but ignore placeholder appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-jquery-serialize-form-into-json-object-but-ignore-placeholder.html

No comments:

Post a Comment