HTML | DOM lang Property

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.