jQuery

Hello guys! I have a question about a function on my page. I use this code snipper which is in JavaScript but it uses jquery. I have to load a big ass jquery lib for this little function on my webpage that i dont want to. This is a language switcher code wich is switching all the div words on the page to the choosen one. So the question is, can i do that somehow without any external library?
Here is the code:

/* Variable to store the different languages for the words. */

var arrLang = {
  "HU": {
    "Bejel" : "Bejelentkezés",
    "Nyelvek" : "Nyelvek",
    "ido" : "A Pontos Idő:",
    "Kezdőlap" : "Kezdőlap",
  },
  "EN": {
    "Bejel" : " Log in",
    "Nyelvek" : "Languages",
    "ido" : "The current Time:",
    "Kezdőlap" : "Home Page",
  },
  "DE": {
    "Bejel" : "Einloggen",
    "Nyelvek" : "Sprachen",
    "ido" : "Die genaue Zeit:",
    "Kezdőlap" : "Startseite",
  },
  "RO": {
    "Bejel" : "Logare",
    "Nyelvek" : "Limbile",
    "ido" : "Timpul exact:",
    "Kezdőlap" : "Pagina principala",
  }
};


// The default language is Hungarian
var lang = "HU";
// Check for localStorage support
if('localStorage' in window){
var usrLang = localStorage.getItem('uiLang');
if(usrLang) {
lang = usrLang
}
}
console.log(lang);
$(document).ready(function() {
$(".lang").each(function(index, element) {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
});
// get/set the selected language
$(".translate").click(function() {
var lang = $(this).attr("id");
// update localStorage key
if('localStorage' in window){
localStorage.setItem('uiLang', lang);
console.log( localStorage.getItem('uiLang') );
}
$(".lang").each(function(index, element) {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
});

The basic working method is that if i “push” a button on my webpage like this:
<a href=“angol” class=“translate lang” key=“ENG” id=“EN”><alt=“angol”/>EN</a>
Then based on the “key” i get the ENG language word to the divs with EN id-s.

A couple of possibly helpful links to check out:

http://youmightnotneedjquery.com/

https://css-tricks.com/now-ever-might-not-need-jquery/

1 Like