Skip to content
Related Articles

Related Articles

HTML | DOM lang Property
  • Last Updated : 25 Jul, 2019

In HTML document, the lang property is used to set or return the value of the lang attribute of an element. This attribute specifies the language code of the element’s content. The element attribute takes “en” for English, “ja” for Japanese, “es” for Spanish and so on. The default value of this attribute is unknown.
Syntax:
Get the value of lang:

HTMLElementObject.lang;

Set new value for lang:

HTMLElementObject.lang = language_code;

The language_code specifies the language code for the element’s content. This property returns a string which represents the language of the element’s text.

Example-1: Assign the value of language code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM lang Property
    </title>
</head>
  
<body>
  
    <p id="para1" lang="en">
        Click the button to display 
      the language code of paragraph.
    </p>
  
    <button onclick="myFunction()">
        Display Language
    </button>
  
    <p id="para2"></p>
  
    <script>
        function myFunction() {
            var x = document.getElementById("para1").lang;
            document.getElementById("para2").innerHTML = x;
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before clicking the button:

After clicking the Display Language button:

Explanation:
The above example gets the language code of the first paragraph and assigns that language to the second paragraph.



Example-2: Check the value of language code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM lang Property
    </title>
</head>
  
<body>
  
    <p id="para1" lang="ja">
        Click the button to display
      the language code of paragraph.
    </p>
  
    <button onclick="myFunction()">
        Display Language
    </button>
  
    <p id="para2"></p>
  
    <script>
        function myFunction() {
            if (document.getElementById("para1").lang === "ja") {
                document.getElementById("para2").innerHTML =
                    "Japanese";
            } else {
                document.getElementById("demo").innerHTML =
                    "Not in Japanese";
            }
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before clicking the button

After clicking the Display Language button

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :