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;
Property Value: 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.
Return Value: A String, representing the language of the element’s text
Example-1: Assign the value of 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() { var x = document.getElementById("para1").lang; document.getElementById("para2").innerHTML = x; } </ script > </ body > </ html > |
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.
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:
Before clicking the button
After clicking the Display Language button
Supported Browsers:
- Google Chrome 1
- Edge 12
- Internet Explorer 5.5
- Firefox 1
- Opera 12.1
- Safari 3
Please Login to comment...