Recently, I encountered such a problem in a project. I need to count the text entered by the user in the text box and display it below, because we are doing a SMS sending platform. Now our country’s SMS service, if your information exceeds 70 characters, the SMS will be sent to you by two. So we are asked to show the user how many words he has entered. It is so easy for users to know how many pieces of information I will give.
I put a code in the network and put it on. When I started using it, it was OK, but after using it for a while, I found a problem. After you delete the text, there is a problem with the word count change above. Later, I studied it and found the problem. onKeyDown="showLen(this)" onKeyUp="showLen(this) In the past, only one of the methods was used, and those who did not use them. These two methods were counted when you press the keyboard and release the keyboard. OK, I put the program on it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <mce:script language="javascript"><!-- function showLen(obj) { document.getElementById("contentLen").value=obj.value.length; document.getElementById("smsnum").value=Math.floor(obj.value.length/70)+1; } // --></mce:script> </HEAD> <BODY> <textarea name="content" cols="60" rows="5" id="content" onKeyDown="showLen(this)" onKeyUp="showLen(this)">