Javascript records the number of characters in the text box to detect the number of characters
I recently encountered such a problem in a project. I want to count the text entered by users in the text box and display it below, because we are working on an SMS sending platform, if your text message service in our country contains more than 70 characters, the text message will be sent to you in two ways. Therefore, we need to show the user the number of words he entered. So that the user can know how many pieces of information will be provided.
I put a code on the Internet and started using it. It was okay, but I found the problem after using it for a while. That is, after you delete the text, there is a problem with the number of words in the text above. Later, I studied it and found the problem. OnKeyDown = "showLen (this)" onKeyUp = "showLen (this) used only one of the methods each time, and none of them were used, the two methods are used to count when you press the keyboard and release the keyboard. Well, I will put the program up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<! 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)"> |