I am a jQuery/JavaScript beginner and trying to create a spreadsheet-style form with validation so that, once information has been entered for any field in the row, all fields in that row should be required too. This post and one of the suggestions posted there were extremely helpful in terms of how to handle validation of text input fields in the row. However, I also need to apply that validation logic to select fields in the row as well. I've tried changing the settings in the .on() and .find() functions, but am having no luck. How could this script (taken from the post above) be changed to handle the select fields in the row as well as the text input fields? Here is a demo with the row validation working for the text inputs, but not the select fields. Thank you!
$("#mytable").on("input", "input", function(){
var anySet = false,
$cells = $(this).closest("tr").find("td input");
$cells.each(function() {
var somethingInCell = $(this).val().trim() !== '';
anySet |= somethingInCell;
});
$cells.removeClass("has-error");
if (anySet) {
$cells.each(function() {
if ($(this).val().trim() === '') {
$(this).addClass("has-error");
}
});
}
});