Reputation: 750
Currently i am working on a site that has multiple language, i want to add that language bar on that site. But my problem is- it keeps saying english+uk icon inside the box, even when it is another language, it should that the appropriate icon+language name instead.
Simply i want to change innerhtml code by following html lang value. I have just basic knowledge on JavaScript.
Live site- http://uposonghar.com/lang.html
My code-
<nav role="custom-dropdown" id="nav_language" dir="ltr">
<input type="checkbox" id="button">
<label for="button" id="labelLang" onclick></label>
<ul>
<li><a hreflang="en" href="http://chitrchatr.com/en/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/en_US.png" title="English" alt="English" /> English</a></li>
<li><a hreflang="de" href="http://chitrchatr.com/de/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/de_DE.png" title="Deutsch" alt="Deutsch" /> Deutsch</a></li>
<li><a hreflang="ru" href="http://chitrchatr.com/ru/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ru_RU.png" title="Русский" alt="Русский" /> Русский</a></li>
<li><a hreflang="es" href="http://chitrchatr.com/es/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/es_ES.png" title="Español" alt="Español" /> Español</a></li>
<li><a hreflang="zh" href="http://chitrchatr.com/zh/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/zh_CN.png" title="中文 (中国)" alt="中文 (中国)" /> 中文 (中国)</a></li>
<li><a hreflang="ar" href="http://chitrchatr.com/ar/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ar.png" title="العربية" alt="العربية" /> العربية</a></li>
<li><a hreflang="fr" href="http://chitrchatr.com/fr/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/fr_FR.png" title="Français" alt="Français" /> Français</a></li>
<li><a hreflang="it" href="http://chitrchatr.com/it/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/it_IT.png" title="Italiano" alt="Italiano" /> Italiano</a></li>
<li><a hreflang="in" href="http://chitrchatr.com/in/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/id_ID.png" title="Bahasa Indonesia" alt="Bahasa Indonesia" /> Bahasa Indonesia</a></li>
<li><a hreflang="ko" href="http://chitrchatr.com/ko/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ko_KR.png" title="한국어" alt="한국어" /> 한국어</a></li>
<li><a hreflang="th" href="http://chitrchatr.com/th/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/th.png" title="ไทย" alt="ไทย" /> ไทย</a></li>
<li><a hreflang="tl" href="http://chitrchatr.com/tl/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/tl.png" title="Tagalog" alt="Tagalog" /> Tagalog</a></li>
<li><a hreflang="hi" href="http://chitrchatr.com/hi/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/hi_IN.png" title="हिन्दी" alt="हिन्दी" /> हिन्दी</a></li>
<li><a hreflang="ja" href="http://chitrchatr.com/ja/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ja.png" title="日本語" alt="日本語" /> 日本語</a></li>
<li><a hreflang="sv" href="http://chitrchatr.com/sv/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/sv_SE.png" title="Svenska" alt="Svenska" /> Svenska</a></li>
</ul>
</nav>
Javascript-
<script>
if(document.getElementsByTagName('html').getAttribute('lang') = "en-US"){
document.getElementById('labelLang').innerHTML = '<img alt="English" title="English" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/en-US.png"> English';
}
if(document.getElementsByTagName('html').getAttribute('lang') = "de-DE"){
document.getElementById('labelLang').innerHTML = '<img alt="Deutsch" title="Deutsch" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/de_DE.png"> Deutsch';
}
if(document.getElementsByTagName('html').getAttribute('lang') = "ru-RU"){
document.getElementById('labelLang').innerHTML = '<img alt="Русский" title="Русский" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ru_RU.png"> Русский';
}
else{}
</script>
Upvotes: 0
Views: 708
Reputation: 1321
You are not closing the tag for <img>
in your innerHTML
.
Also,
document.getElementsByTagName('html').getAttribute('lang')
= "en-US"
is an assignment not equality check. Use ==
instead.
As pointed out by @Akamaozu document.getElementsByTagName()
returns HTMLCollection
.
Upvotes: 2
Reputation: 705
document.getElementsByTagName
returns an array of elements that match the tag, even if there is only one match.
What you need to do is document.getElementsByTagName('html')[0]
to get the first tag returned by the function, which should be your HTML tag.
Also if you want to compare, ==
or ===
is what you're looking for. =
just sets a value.
Upvotes: 2