0

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" />&nbsp;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" />&nbsp;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="Русский" />&nbsp;Русский</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" />&nbsp;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="中文 (中国)" />&nbsp;中文 (中国)</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="العربية" />&nbsp;العربية</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" />&nbsp;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" />&nbsp;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" />&nbsp;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="한국어" />&nbsp;한국어</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="ไทย" />&nbsp;ไทย</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" />&nbsp;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="हिन्दी" />&nbsp;हिन्दी</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="日本語" />&nbsp;日本語</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" />&nbsp;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">&nbsp;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">&nbsp;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">&nbsp;Русский';
}
else{}
</script>

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.