I recently encountered such a problem while working on a project. It is necessary to count the text entered by the user in the text box and display it below. Because what we are doing is an SMS sending platform, and now our country’s SMS service, If your message exceeds 70 characters, two text messages will be sent to you. So we are required to show the user how many words he has entered. It is convenient for users to know that my information will be divided into several pieces.
I downloaded a code and uploaded it. When I started using it, it was ok and not bad, but after using it for a while, I discovered a problem. It means that after you delete the text, there is a problem with the change in word count. Later, I did some research and found the problem. onKeyDown="showLen(this)" onKeyUp="showLen(this). In the past, only one of the methods was used each time, and no one used it. These two methods count when you press the keyboard and release the keyboard. Okay, I’ll upload the program
<!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)">