سؤال

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');
                }
هل كانت مفيدة؟

المحلول

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

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

toggleClass() docs

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top