I know this question was asked more than million times but there's a gap I can't fill yet.
I have a pretty basic navbar structure like this:
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<%= link_to "Entry", entry_path, class: 'nav-link', 'data-turbolinks' => false %>
</li>
<li class="nav-item">
<%= link_to "Entry 2", entry2_path, class: 'nav-link', 'data-turbolinks' => false %>
</li>
<li class="nav-item localization-links">
<% unless I18n.locale == :en %>
<div class="unactive-localization-button">
<%= link_to "EN", locale: :en %>
</div>
<% else %>
<a class: 'nav-link'>EN</a>
<% end %>
</li>
<li class="nav-item localization-links">
<% unless I18n.locale == :fr %>
<%= link_to "FR", locale: :fr %>
<% else %>
<a class: 'nav-link'>FR</a>
<% end %>
</li>
</ul>
My actual structure is more complex so I reduced most of the code like other links and conditions for better readability. I change active class like this:
$(".nav li").click(function() {
if ($(".nav li").hasClass("active")) {
$(this).removeClass("active");
} else {
$(this).addClass("active");
}
});
And it works for all the links ('Entry 1', 'Entry 2', etc) except of my localization links. All I want is to give active class to these links but somehow nothing I read so far worked for me (I used this thread to check where I went wrong). I have no console errors when I change localization and I obviously add 'active' class manually in browser's editor, but not with my code. Any thoughts why? Thanks in advance for your time, I appreciate any comment or answer!