PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] How to call document with lang="en" to javascript?

 July 01, 2022     html, javascript, shopify     No comments   

Issue

I'm trying to create a conditional styling on javascript by calling this line found before the head and body tag:

<html class="js" lang="en">

I need to create a condition if the lang === "en" will display an image I hid with english content on it. I also have lang="de" which is another image with deutsch content hidden too.

Both images are linked in an <a> tag and if the site is in english, the english image will appear once clicked, if it's in german, the deutsch image will appear once the link is clicked. I had the link in modal to pop up the image once clicked.

Here's the image html code:

<a>
    <p><img> English Image FAQs</p>
    <p><img> Deutsch Image FAQs</p>
</a>

I hope I'm making sense. Thank you


Solution

Take a look at this, I guess this is what you want:

const setLang = (lang)=> document.getElementsByTagName("html")[0].setAttribute("lang", lang);
setLang(navigator.language.slice(0,2))
.en, .de {
  display: none;
}
html[lang="en"] .en {
  display: block;
}

html[lang="de"] .de {
  display: block;
}
<a>
    <p class="en"><img src="https://via.placeholder.com/150?text=en"> English Image FAQs</p>
    <p class="de"><img src="https://via.placeholder.com/150?text=de"> Deutsch Image FAQs</p>
</a>

<a onclick="setLang('de')">de</a>
<a onclick="setLang('en')">en</a>



Answered By - jns
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing