2

I am pulling a text from a JavaScript file but the HTML tags don't work.

I followed the best answer here and ran into that problem:
Build multiple language website using jQuery and JSON based methods

This is the part of the JavaScript file:

    var arrLang = {
    "en-gb": {
        "HOME": "Home",
        "ABOUT": "About <br> Us",
        "CONTACT": "Contact Us",
    },
    "zh-tw": {
        "HOME": "首頁",
        "ABOUT": "關於我們",
        "CONTACT": "聯絡我們",
    }
}; 
    
    // The default language is English
    var lang = localStorage.getItem('lang') || navigator.language.slice(0, 2);
    if (!Object.keys(arrLang).includes(lang)){
        lang = 'en';
    }
    // Check for localStorage support
    if('localStorage' in window){
       
       var usrLang = localStorage.getItem('uiLang');
       if(usrLang) {
           lang = usrLang;
       }
    
    }
    
    
    console.log(lang);
    
            $(document).ready(function() {
                 "use strict";
              $(".lang").each(function() {
                $(this).text(arrLang[lang][$(this).attr("key")]);
              });
            });
    
            // get/set the selected language
            $(".language-button").click(function() {
                 "use strict";
              var lang = $(this).attr("id");
              localStorage.setItem('lang', $(this).attr("id"));
              
    
              // update localStorage key
              if('localStorage' in window){
                   localStorage.setItem('uiLang', lang);
                   console.log( localStorage.getItem('uiLang') );
              }
    
              $(".lang").each(function() {
                $(this).text(arrLang[lang][$(this).attr("key")]);
              });
            });

HTML: This is how I pull the Text:

<div class="lang" key="ABOUT"></div>

HTML: Expected result:

About
Us

HTML: This is what I get:

About <br> Us

What is a very simple way to make the HTML tags work in this case?

5
  • Please place inside <pre></pre> tag Commented Jun 13, 2021 at 9:18
  • How did you pull out the text from arrLang on page ? Commented Jun 13, 2021 at 9:21
  • do it - $(this).html(arrLang[lang][$(this).attr("key")]); Commented Jun 13, 2021 at 9:46
  • 1
    @s.kuznetsov Thank you very much, this was the solution! I now have learned that text only transfers the plain text, ignoring the html tags. Have a very nice day! Commented Jun 13, 2021 at 9:58
  • @Blacksite, no problem. Good day to you, too. Commented Jun 13, 2021 at 10:10

2 Answers 2

0

If you use use .innerText in your JavaScript file to put the values into the HTML file, you might want to use .innerHTML instead. This will help if you have tags (like the <br> tag in your situation) in your values.

If you use something else to put the values into the HTML file, please clarify which method you use...

You can find more information about innerHTML vs innerText here.

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

3 Comments

Thank you, I have added the entire JS code, but it seems as innerHTML is not used anywhere
Indeed, I saw you already found the solution. Good luck with your project!
Thank you very much! Have a very nice day :)
0

you're not specifying how you do the replacement, but given your data

var arrLang = {
  "en": {
    "HOME": "Home",
    "ABOUT": "About <br> Us",
    "CONTACT": "Contact Us",
  },
};

you could simply have:

var arrLang = {
  "en": {
    "HOME": "Home",
    "ABOUT": "About <br> Us",
    "CONTACT": "Contact Us",
  },
};

var languageSelected = 'en';
var langData = arrLang[languageSelected];

// for each key in your object
Object.keys(langData).forEach(k => {
  // search for attribute "key" and add string as HTML
  $(`[key=${k}]`).html(langData[k]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul>
  <li><div class="lang" key="HOME"></div></li>
  <li><div class="lang" key="ABOUT"></div></li>
  <li><div class="lang" key="CONTACT"></div></li>  
</ul>

2 Comments

Thank you, I have added the whole JS code. I am stil trying out your answer to make it work somehow
Thank you I saw you have also used '.html' which in my case was the solution :) Have a nice day!

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.