JS implementation of dynamic prompt text box to enter the remaining words (similar to the publication of micro-blog digital Tips)

Source: Internet
Author: User

First, the realization effect:

In order to more intuitively reflect the user in the text box input text can see how many words they entered, the project needs to be judged by the text box remaining to enter the number of words.

HTML & JS:

1 <Div>2 <textareaname= "Content"ID= "Content"onkeyup= "Javascript:checkwords (this);"
OnMouseDown= "Javascript:checkwords (this);" ></textarea>3 </Div>4 <Div> 5You can also enter<spanstyle= "Font-family:georgia; font-size:26px;"ID= "Wordcheck">80</span>a kanji6 </Div> 7 8 <Script>9 varMaxstrlen= the;Ten functionQ (s) { One returndocument.getElementById (s); A } - functionCheckwords (c) { - Len=Maxstrlen; the varStr=C.value; - Mylen=Getstrleng (str); - varWck=Q ("Wordcheck"); - if(Mylen>Len*2){ + C.value=str.substring (0, I+1); - }Else{ + wck.innerhtml=Math.floor (Len*2-Mylen)/2); A } at } - functionGetstrleng (str) { - Mylen= 0; - I= 0; - for(;(i<str.length)&&(Mylen<=Maxstrlen*2); I++){ - if(Str.charcodeat (i)>0&&str.charcodeat (i)< -) in Mylen++; - Else to Mylen+=2; + } - returnMylen; the }


The above can be dynamically judged text box can enter the remaining words, data from the network.

JS implementation of dynamic prompt text box to enter the remaining words (similar to the publication of micro-blog digital Tips)

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.