Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does JS implement a reminder of the number of words remaining in the input box text? (With source code)


May 29, 2021 Article blog



When we develop a Web page, we often involve the development of input boxes. D ue to the limitations of database character fields, the amount of text is often limited. I f you add a reminder of the remaining characters next to the input box, you can enhance the user experience. S o how does JS implement a reminder of the number of words remaining in the input box text? This article tells you.

Let's start with the results:

 How does JS implement a reminder of the number of words remaining in the input box text? (With source code)1

The source code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>W3Cschool-编程狮</title>
	<script type="text/javascript" src="./js/jquery.min.js"></script>
</head>
<script type="text/javascript">
$().ready(function(){
var ty = document.getElementById("cty");
var sentence=document.getElementById("sentence");
var minLen=document.getElementById("minLen");
var maxLen=document.getElementById("maxLen");
var leftLen=document.getElementById("leftLen");
sentence.onkeyup= function () {
maxLength = parseInt(maxLen.firstChild.nodeValue);
this.value = this.value.substr(0,maxLength);
var len = this.value.length;
minLen.firstChild.nodeValue = len;
leftLen.firstChild.nodeValue = maxLength-len;
};
});
</script>
<body>
<label>
<textarea name="sentence" id="sentence"></textarea>
<br/>
<em>
<span id="minLen">0</span>/<span id="maxLen">100</span>字,还能输入<span id="leftLen">100</span>字!
</em>
</label>
</body>
</html>

That's how JS implements the full number of words remaining in the input box text.