My function counts the number of cards visible on the screen and if all are shown, I add thearrow-up
class to my icon, if some cards are still hidden from the user,arrow adds -down
icon.
const showMoreIcon = document.querySelector('.cta-icon'); function myFunction(){ const btnIcon = cardsOnShow >= cards.length ? 'arrow-up' : 'arrow-down'; showMoreIcon.classList.add(btnIcon); }
This works, but I can see in the DOM that the correct class is added to thespan
as I would expect it to be since thearrow-down
class was added first (in The user has to expand the content multiple times before all visible cards are shown) - then, although thearrow-up
class is added, it does not override thearrow-down
.p>
How to ensure that whenarrow-up
is added,arrow-down
is removed and vice versa? I've previously usedtoggle
to implement a simple open/close icon, but this doesn't work as it may expand multiple times before closing.
I recommend deleting courses you no longer need:
Cascading takes into account many factors a>, which is a set of rules used to determine which rule "wins" when two conflicting rules are applied to the same element.
The order in which classes are defined on HTML elementsis not one of these factors.
and
are the same as far as cascading is concerned.
Deleteclasses that should not be applied.
Or write the CSS in a way that only cares about a single class to change the orientation:
Then