I've started using BEM methodology to decouple my HTML and CSS ... and it works pretty well most of the time. Even if its only your personal opinion, i would still like to know how others deal with this:
Let's assume we need to build a simple navigation. The HTML looks similar to
<nav class="nav">
<ul class="nav__list">
<li class="nav__item">
<a class="nav__link" href=""></a>
</li>
<li class="nav__item">
<a class="nav__link" href=""></a>
</li>
</ul>
</nav>
I'm not sure if i need the ".nav_item" and ".nav_link" or if it's better pratice to use this instead
.nav__list > li { CODE }
But my real issue is how to deal with "active" classes (not just for navigations, but in general). Is it better to use specific "active" classes like ".nav_item--active", so you can just use a single class inside your CSS file or if using more general class names like ".is-active" works better? But then you need to specify your classes inside your CSS file like ".nav_item.is-active" or (which looks even worse to me) ".nav__list > .is-active".
Every method has its downsides. To me the second way looks wrong if using BEM, but if you are going for the first way you run into "troubles" with your JS, because you need to "hard-code" the specific class name into your JS
someElement.addClass(".nav__item--active");
That way your JS relies too much on your HTML structure (or doesn't this matter too much?), which might change... And this leads to the second question. I heard that it's good to decouple not only your HTML and CSS but also your HTML and JS. So you could for example use those ".js-" classes to add click events and all that kind of stuff to elements instead of using your "styling" classes to trigger those kind of events. So instead of using
<button class="btn btn--large"></button> // $(".btn--large") in jQuery
you would have
<button class="btn btn--large js-dostuff"></button> // $(".js-dostuff") in jQuery
I think this in combination with HTML5 data-attributes works for pretty much for anything, but i'm asking myself what happens to navigation or accordions or stuff like that. Is it better for maintainability to use those ".js-" classes as well (for every item)
<nav class="nav">
<ul class="nav__list">
<li class="nav__item js-open-subnav">
<a class="nav__link" href=""></a>
<ul class="nav__sub">
<!-- ... -->
</ul>
</li>
</ul>
</nav>
or should i use $(".nav__item")... in my JS in this case? But that way you don't really decouple your HTML and JS (at least as far i understood this topic). It's not just about navigations, but about all those kind of javascript interactions, like accordions, sliders and so on.
I'll hope you guys can share some best practices for those questions and help me out.
Thanks