JavaScript implementation Statistics text box textarea words enhance user experience _javascript skills

Source: Internet
Author: User
Now popular Twitter and other micro-blogging sites, there is a good user experience, that is, in the text box input text, will automatically statistical input characters, and display the user can also enter the characters, in the limited 140 words in the micro-blog, such a small hint can be very good to enhance the user experience.

If this technology is implemented, I did some research, found that the implementation is actually quite simple, a few lines of code can complete the input character statistics, after the actual test, the statistics of the text and Twitter and other micro-blogs are the same.

The way to do this is to add a span to display the remaining words, and then in textarea, add a onkeydown and onkeyup event, call another JavaScript function, and the function call parameter is the span ID and the textarea ID. Then use innerHTML in JavaScript to return the calculated remaining words.

the following are the relevant JavaScript codes
The following are the referenced contents:
Copy Code code as follows:

<script language= "javascript" >
function Countchar (textareaname , Spanname)
{
document.getElementById (spanname). InnerHTML = 140-document.getelementbyid (textareaname). Value . length;
}
</script>
can enter <span id= "counter" >140</span> word <br/>
<textarea id= "status "Name=" status "rows=" 6 "cols=" Onkeydown= ' Countchar ("status", "Counter"); ' onkeyup= ' Countchar ("status", "Counter" );' ></textarea>
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.