The HTML DOM lang property returns/sets a string depicting a language code such as ‘en’ for ‘english’ of the element’s content.
Syntax
Following is the syntax −
- Returning language code
HTMLElementObject.lang
- Setting lang attribute to a language code
HTMLElementObject = ‘lang_code’
Example
Let us see an example for HTML DOM lang property −
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM lang</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-lang</legend>
<label for="langSelect">Editor:
<input type="text" size="25" lang="en" id="langSelect" value="This is a learning tutorial">
</label>
<input type="button" onclick="updateValue()" value="Translate to Spanish">
<div id="divDisplay">Currently accepted language :</div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputlang = document.getElementById("langSelect");
divDisplay.textContent += langSelect.lang;
function updateValue() {
inputlang.lang = 'es';
inputlang.value = 'Este es un tutorial de aprendizaje';
divDisplay.textContent = 'Currently accepted language : '+langSelect.lang;
}
</script>
</body>
</html>Output
This will produce the following output −
Before clicking ‘Translate to Spanish’ button −

After clicking ‘Translate to Spanish’ button −
