Home >Web Front-end >JS Tutorial >Count the number of words in Textarea in JavaScript and prompt for characters that can still be entered_javascript skills

Count the number of words in Textarea in JavaScript and prompt for characters that can still be entered_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:45:011268browse

Nowadays, popular microblogging websites such as Twitter have a good user experience. When entering text in the text box, the entered characters will be automatically counted and the characters that the user can still enter will be displayed. This is limited to 140 characters. In microblogs, small tips like this can greatly enhance the user experience.


What if this technology is implemented? I did some research and found that the implementation is actually quite simple. A few lines of code can complete the input character statistics function. After actual testing, its text statistics are similar to Twitter Waiting for the micro blog is exactly the same.

The method of use is to first add a span to display the remaining number of words, then add an onkeydown and onkeyup event in the Textarea, and call another JavaScript function. The parameters of the function call are the id and span of the span. The id of the textarea, and then use innerHTML in JavaScript to return the calculated remaining word count.

Core Javascript code:

Copy code The code is as follows:

< span style="font-size:18px;">
You can enter 140words

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn