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

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

Vue implements carousel animation

This article example shares the specific code of ...

Detailed explanation of the use of props in React's three major attributes

Table of contents Class Component Functional Comp...

Detailed explanation of Vue custom instructions

Table of contents Vue custom directive Custom dir...

How to modify the user and group of a file in Linux

In Linux, when a file is created, the owner of th...

Summarize how to optimize Nginx performance under high concurrency

Table of contents Features Advantages Installatio...

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

Introduction to Sublime Text 2, a web front-end tool

Sublime Text 2 is a lightweight, simple, efficien...

How to install and use Server-U 14 version

Introducing Server-U software Server-U is a very ...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

Javascript to achieve the drag effect of the login box

This article shares the specific code of Javascri...

Detailed explanation of mkdir command in Linux learning

Table of contents Preface 1. Basic knowledge of f...