How to get the dynamic number of remaining words in textarea

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never written about before. I spent half the afternoon working on it and finally wrote it out. I felt very accomplished! Of course, this is nothing for JS experts, but it is a small step forward for my own JS ability.

Case introduction: We often see that some websites have textarea text boxes. When you type, there will be text prompts below to show how many more words you can enter. Today we are going to implement this function. Of course, since there are several textareas on a page, it is not possible to use a single js logic to control them, so they have to be encapsulated a little. Of course, there are still some omissions in my encapsulation, but the basic functions are achieved.

First, let's introduce a single textarea implementation case

HTML part:

XML/HTML CodeCopy content to clipboard
  1. < textarea   id = "text_txt1" > </ textarea >   
  2. < span   id = "num_txt1" > 600 characters remaining </ span >   

js part:

JavaScript CodeCopy content to clipboard
  1. $( function () {
  2. $( '#text_txt1' ).on( 'keyup' , function (){
  3.         var txtval = $( '#text_txt1' ).val().length;
  4. console.log(txtval);
  5.        var str = parseInt(600-txtval);
  6. console.log(str);
  7.          if (str > 0 ){
  8. $( '#num_txt1' ).html( 'The rest can be entered' +str+ 'characters' );
  9. } else {
  10. $( '#num_txt1' ).html( '0 characters remaining' );
  11. $( '#text_txt1' ).val($( '#text_txt1' ).val().substring(0,600)); //This means that when the text inside is less than or equal to 0, the number of words cannot be increased and can only be 600 words   
  12. }
  13.          //console.log($('#num_txt').html(str));   
  14. });
  15. })

Then introduce the implementation cases of multiple textareas on the same page

JavaScript CodeCopy content to clipboard
  1. function changeLength(obj,num){
  2. obj.on( 'keyup' , function (){
  3.      var txtval = obj.val().length;
  4.          //console.log(txtval);   
  5.          var str = parseInt(600-txtval);
  6.          //console.log(str);   
  7.          if (str > 0 ){
  8. num.html( 'remaining input' +str+ 'words' );
  9. } else {
  10. num.html( '0 characters remaining' );
  11. obj.val(obj.val().substring(0, 600));
  12. }
  13.          //console.log($('#num_txt').html(str));   
  14. });
  15. }
  16. $( function (){ // I have four here, so call 4 times   
  17. changeLength($( '#text_txt1' ),$( '#num_txt1' ));
  18. changeLength($( '#text_txt2' ),$( '#num_txt2' ));
  19. changeLength($( '#text_txt3' ),$( '#num_txt3' ));
  20. changeLength($( '#text_txt4' ),$( '#num_txt4' ));
  21. });

Of course, the number of words actually required here can also be encapsulated inside the function, but I won’t do that. In this way, when you enter text, the remaining number of words will be automatically displayed inside the span. When the input value reaches the maximum value, the remaining number of words will be displayed as 0, and new content cannot be filled in. When deleting text, span can dynamically obtain the number of remaining words.

Below is the code of others. This time, I also borrowed some of their writing methods.

html:

XML/HTML CodeCopy content to clipboard
  1. < div   class = "family_v2" >   
  2.      < p   class = "nickname_v2" > Introduction: </ p >   
  3.       < textarea   id = "content"   name = "sign"   style = "height:60px;overflow-y:hidden;"   
  4.       onkeyup = "changeLength(this,60)"   class = "nicknameBox_v2 brief_box_v2" >   
  5.       </ textarea >   
  6.       < div   class = "limit_num_v2" >   
  7.           < h3 > 60 </ h3 >   
  8.      </ div >   
  9.   </ div >   

js:

JavaScript CodeCopy content to clipboard
  1. //Verify the length of the textarea   
  2. function changeLength(obj,lg){
  3.      var len = $(obj).val();
  4. $(obj).next().find( "h3" ).text(lg-len.length);
  5.      if (len.length>=lg){
  6. $(obj).next().find( "h3" ).text(0);
  7. $(obj).val(len.substring(0,lg));
  8. }
  9. }

The above article on how to obtain the dynamic number of remaining words in textarea is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

<<:  Example of fork and mutex lock process in Linux multithreading

>>:  How to create dynamic QML objects in JavaScript

Recommend

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing can be ...

Vue conditional rendering v-if and v-show

Table of contents 1. v-if 2. Use v-if on <temp...

Detailed explanation of three relationship examples of MySQL foreign keys

This article uses examples to describe the three ...

JavaScript code to implement Weibo batch unfollow function

A cool JavaScript code to unfollow Weibo users in...

Linux parted disk partition implementation steps analysis

Compared with fdisk, parted is less used and is m...

The process of installing MySQL 8.0.26 on CentOS7

1. First, download the corresponding database fro...

JS operation object array to achieve add, delete, modify and query example code

1. Introduction Recently, I helped a friend to ma...

Explanation of the problem that JavaScript strict mode does not support octal

Regarding the issue that JavaScript strict mode d...

Security considerations for Windows server management

Web Server 1. The web server turns off unnecessar...

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

Usage instructions for the docker create command

The docker create command can create a container ...