Count the number of Textarea characters in JavaScript and prompt the characters that can be entered

Source: Internet
Author: User

The popular micro-blog websites such as Twitter have a good user experience. When you enter text in the text box, the entered characters are automatically counted, and display the characters that can be entered by the user. In a microblog with a limit of 140 characters, such tips can greatly enhance the user experience.


If this technology is implemented, I did some research and found that the implementation is actually quite simple. After a few lines of code, I can complete the input character statistics function. After actual tests, its text statistics are exactly the same as those of Weibo posts such as Twitter.

You can add a span to display the remaining words. Then, add an onkeydown and onkeyup event to Textarea and call another JavaScript function, the function call parameters are the span id and textarea id, and the remaining words are returned using innerHTML in JavaScript.

Core Javascript code:
Copy codeThe Code is as follows:
<Span style = "font-size: 18px;"> <script language = "javascript">
Function countChar (textareaName, spanName)
{
Document. getElementById (spanName). innerHTML = 140-document. getElementById (textareaName). value. length;
}
</Script>
You can enter <span id = "counter"> 140 </span> words <br/>
<Textarea id = "status" name = "status" rows = "6" cols = "40" onkeydown = 'countchar ("status", "counter ");'
Onkeyup = 'countchar ("status", "counter"); '> </textarea> </span>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.