I am struggling with optimizing this JavaScript code that adds and removes classes based on the presence of a specific class in the next rows of a table. Can anyone provide some guidance on how to make this code more efficient?
$(".showTR").click(function () {
let currentRow = $(this).parent().parent('tr');
for (let i = 0; i < currentRow.length; i++) {
if (!currentRow.next("tr").hasClass('Row')) {
currentRow.removeClass('Row');
currentRow = currentRow.next('tr');
} else {
break;
}
}
if(!currentRow.hasClass('Row')) {
$(this).hide();
}
});
This code is designed to iterate through each row in a table, checking if the next row has a specific class. If it does not, the class is removed from the current row and the loop continues until a row with the specified class is found.