I have the following function, it works exactly how I need it to; however I think there is room for improvement.
The script is supposed to loop through all input and text area html elements. It should only work with elements that have a name, and a value, and it should ignore "hidden". However some of the elments do not have a type attribute, so I can't do something like input.attr('type').toLowerCase() != 'hidden'
.
After that, it ignores any field with the class element_reference_input
, and looks for fields with form-control or question_textarea_input
.
Last it writes the values to an array.
jQuery('input, textarea').each(function(index){
var input = jQuery(this);
if (input.val() && input.attr('name') && input.attr('type') != 'hidden' && input.attr('type') != 'HIDDEN') {
if (input.attr('class')) {
elmClass = input.attr('class');
}
if (elmClass.indexOf('element_reference_input') <= -1 && (elmClass.indexOf('form-control') > -1 || elmClass.indexOf('question_textarea_input') > -1 )) {
objOutgoingData.push(
{
name:'' + input.attr('name').split('.').pop(),
value:'' + input.val(),
table:'' + tableName,
}
);
}
}
});
I feel that I have to many if statements, I'd like to streamline it. But I am trying to avoid undefined errors if class or type is not present on the html tag.