I wrote this if statement event listener to add and remove a class to a couple links based on changed.owl.carousel. However, I know this is not the most DRY method in writing the code and often find myself writing more than is needed. Is there a cleaner way to write this code?
owl.on('changed.owl.carousel', function(event) {
if ($('.owl-item.active>.item.slide3').length>0) {
$('#link3').addClass('hover');
} else if (jQuery('.owl-item.active>.item.slide4').length>0) {
$('.link').removeClass('hover');
$('#link4').addClass('hover');
} else {
$('.link').removeClass('hover');
}
})
somename*(where * is a number) naming patterns show up in code. This code smell oftentimes indicates that you have places where you can improve your approach. It is hard to say here though without context. \$\endgroup\$linkas both a class name andlink#as an ID. IMO that makes it harder to read, and easier to make a mistake using one when you need the other. \$\endgroup\$