user3002842
user3002842

Reputation: 127

Summarizing a jQuery code

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');
                }

Upvotes: 0

Views: 37

Answers (1)

charlietfl
charlietfl

Reputation: 171669

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

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

toggleClass() docs

Upvotes: 2

Related Questions