Skip to content

Instantly share code, notes, and snippets.

@viniciusban
Last active August 29, 2015 14:04
Show Gist options
  • Save viniciusban/b18bf1464521eb9dedfc to your computer and use it in GitHub Desktop.
Save viniciusban/b18bf1464521eb9dedfc to your computer and use it in GitHub Desktop.
[javascript] Which fields are filled or empty in my form?
var map_which_fields_are_filled_or_not = function (form_id) {
/* for each field on form, recognize if it's filled or not.
Returns:
- {
"field_a": true, // filled
"field_b": false // empty (not filled)
}
*/
var fields = {};
$('#'+form_id).find(':input').filter(':visible').each(function() {
if (!this.name) {
return;
}
if ((this.type === 'checkbox') || (this.type === 'radio')) {
if (!fields[this.name]) {
fields[this.name] = $("#"+this.id).prop('checked');
}
} else {
fields[this.name] = ($("#"+this.id).val().length > 0);
}
});
return fields;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment