At the input event, check the value and, accordingly, add / remove an element in the array.
var reqFields = []; jQuery('label.required').each(function() { console.log(jQuery(this).text()); reqFields.push(jQuery(this).text()); }); jQuery('.custom-field').on('input', function() { if (this.value) { // Remove this from the list/array reqFields.splice(jQuery(this).index(),1); // jQuery(this).index() havent tried, else just compute index some other way } else { // add back if cleared out reqFields.push( jQuery('label.required').eq(jQuery(this).index()).text()); } });
source share