Open In App

HTML DOM lang Property

Improve
Improve
Like Article
Like
Save
Share
Report

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;

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

Return Value: A String, representing the language of the element’s text

Example 1: Assign the value of the language code. 

HTML




<!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() {
            let x = document.getElementById("para1").lang;
            document.getElementById("para2").innerHTML = x;
        }
    </script>
</body>
</html>


Output: 

 

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 the language code. 

HTML




<!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>


Output: 

 

Supported Browsers: 

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 5.5
  • Firefox 1
  • Opera 12.1
  • Safari 3


Last Updated : 13 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads