Use JQuery to calculate the number of input texts in the input and textarea codes. jquerytextarea

Source: Internet
Author: User

Use JQuery to calculate the number of input texts in the input and textarea codes. jquerytextarea

This article mainly introduces jQuery's method for counting the number of input texts. For more information, see.

HTML section:

<Input type = "text" value = "I am the input text" maxlength = "10" id = "detail1"/> <p> <span id = "detail1_num"> 0 </span>/<span> 10 </span> </p> <textarea placeholder = "enter up to 100 words" rows = "3" maxlength = "100" class =" editDetail "id =" detail2 "> Haha </textarea> <p> <span id =" detail2_num "> 0 </span>/<span> 100 </span> </ p>

JS section:

1 $ (function () {2/* input words */3/* The number of words in the input box is displayed first when the page is loaded */4 var text1 = $ ("# detail1 "). val (); 5 var counter1 = text1.length; 6 $ ("# detail1_num "). text (counter1); 7/* Add a trigger event for dynamic calculation of the number of words in the input box */8 $ ("# detail1 "). on ('blur keyup input', function () {9 var text = $ ("# detail1 "). val (); 10 var counter = text. length; 11 $ ("# detail1_num "). text (counter); 12}); 13/* textarea words */14 var text2 = $ ("# detail2 "). val (); 15 var counter2 = text2.length; 16 $ ("# detail2_num "). text (counter2); 17 $ ("# detail2 "). on ('blur keyup input', function () {18 var text = $ ("# detail2 "). val (); 19 var counter = text. length; 20 $ ("# detail2_num "). text (counter); 21}); 22 });

As follows:

As the blogger is mainly engaged in mobile development, the input box will appear in the input text at the beginning of the mobile testing, and the input text in the input box will be incorrect during computing, therefore, an input event is added, which is calculated by the input side in the input box (mainly after entering the Chinese character ).

I hope to help you.

 

 

The article is original. For reposted content, please mark the address of this article as a link. Thank you.

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.