Home >>Javascript Tutorial >Javascript innerText

Javascript innerText

InnerText in JavaScript

The innerText property in JavaScript is used to write the dynamic text on the HTML document. In this property the text will be not interpreted as html text instead they will be interpreted as normal text.

InnerText Property in JavaScript is mostly used in the web pages in order to generate the dynamic content like writing the validation message, password strength and many more.

Here is an example of the innerText in JavaScript to understand it from a simple point of view:

In the following example, the password strength will be determined:

<script>  
function validate() {  
var msg;  
if(document.myForm.userPass.value.length>5){  
msg="Strong";  
}  
else{  
msg="Weak";  
}  
document.getElementById('mylocation').innerText=msg;  
 } 

 
</script> 
<form name="myForm">  
<input type="password" value="" name="userPass" onkeyup="validate()">  
Strength:<span id="mylocation">no input</span>  
</form>
Output :
Strength:no input

Click the button to get the inner text of the button element.

<button onclick="myFunction()" id="myBtn">Click me</button>
<p id="msg"></p>
<script>
function myFunction() {
  var x = document.getElementById("myBtn").innerText;
  document.getElementById("msg").innerHTML = x;  
}
</script>
Output :

 


No Sidebar ads