Question

This code is not clean. But it works

I need a code clean. Is there a way to convert this code into a clean code there?

var favIcon = $(obj).find('i').first();
                if (favIcon.hasClass('fa-star')) {
                    favIcon.removeClass('fa-star');
                    favIcon.addClass('fa-star-o');
                } else {
                    favIcon.removeClass('fa-star-o');
                    favIcon.addClass('fa-star');
                }
Was it helpful?

Solution

Can use toggleClass() and do it all in one line

$(obj).find('i').first().toggleClass('fa-star fa-star-o');

toggleClass() docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top