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

Causes and solutions for slow MySQL queries

There are many reasons for slow query speed, the ...

How to use nginx to simulate canary release

This article introduces blue-green deployment and...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

Linux kernel device driver kernel debugging technical notes collation

/****************** * Kernel debugging technology...

MySQL transaction details

Table of contents Introduction Four characteristi...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

What command is better for fuzzy searching files in Linux?

1. Introduction This article mainly explains how ...

How to use firewall iptables strategy to forward ports on Linux servers

Forwarding between two different servers Enable p...

Advantages of INSERT INTO SET in MySQL

Insert data into mysql database. Previously commo...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

js to achieve a simple magnifying glass effect

This article shares the specific code of js to ac...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

Vue uses dynamic components to achieve TAB switching effect

Table of contents Problem Description What is Vue...