I am trying to hide unwanted languages from a language selection menu. The relevant HTML code is as follows:
jQuery('.subnav-item').each(function() {
var language = jQuery(this).innerHTML;
var index = $('#customlanguagemenu').data('customlanguages');
if (index.includes(language)) {
jQuery(this).css({'display':'block','fontWeight':'bold','fontSize':'13px'});
} else {
jQuery(this).css('display', 'none');
console.log(this + "This language was removed.");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li role="menuitem" class="subnav-item" tabindex="-1" data-selected>
<a class="submenu-link" tabindex="-1" >English</a>
</li>
<li role="menuitem" class="subnav-item" tabindex="-1" >
<a title="Deutsch" href="/eds/Toolbar/ChangeLanguage?sid=37f38cff-e78a-44ca-9522-8d27966a321@sessionmgr103&vid=0&theDb=de&theContentType=de" class="submenu-link" tabindex="-1" >Deutsch</a>
</li>
<li role="menuitem" class="subnav-item" tabindex="-1" >
<a title="Español" href="/eds/Toolbar/ChangeLanguage?sid=37f38cff-e78a-44ca-9522-98d27966a321@sessionmgr103&vid=0&theDb=es&theContentType=es" class="submenu-link" tabindex="-1" >Español</a>
</li>
<li role="menuitem" class="subnav-item" tabindex="-1" >
<a title="Ελληνικά" href="/eds/Toolbar/ChangeLanguage?sid=37f38cff-e78a-44ca-9522-98d27966a321@sessionmgr103&vid=0&theDb=el&theContentType=el" class="submenu-link" tabindex="-1" >Русский</a>
</li>
<script id="customlanguagemenu" type="text/javascript" src="javascript.js" data-customlanguages='["English","Русский"]'></script>
I can see that the code is executed, but ALL languages are disappearing, not just the ones selected in the data-customlanguages attribute.
What am I missing here?
I tried using .text() instead of innerHTML before, but it did not change the outcome. I also tried applying the display:none to the a elements instead of the li elements (since the innerHTML actually sits in the a elements) but the result still did not change.