How to obtain the dynamic remaining word count of textarea
This time I will show you how to obtain the dynamic remaining word count of textarea, and what are the precautions for obtaining the dynamic remaining word count of textarea. The following is a practical case, let's take a look.
I encountered a case at work that I have never written about before. I spent half an afternoon trying to write it out. It feels so gray but also feels a sense of accomplishment! Of course, this is nothing to a js expert, 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 enter, there is a text prompt below how many words you can enter. Today we are going to implement this function. Of course, since a page has several textareas, it is not possible to use a single js logic for control, and it must be encapsulated in a small way. Of course, there are still some flaws in my package, but the basic functions are achieved.
First introduce a single textarea implementation case
html part:
<textarea id="text_txt1"></textarea> <span id ="num_txt1">剩余可输入600字</span>
js part:
$(function(){ $('#text_txt1').on('keyup',function(){ var txtval = $('#text_txt1').val().length; console.log(txtval); var str = parseInt(600-txtval); console.log(str); if(str > 0 ){ $('#num_txt1').html('剩余可输入'+str+'字'); }else{ $('#num_txt1').html('剩余可输入0字'); $('#text_txt1').val($('#text_txt1').val().substring(0,600)); //这里意思是当里面的文字小于等于0的时候,那么字数不能再增加,只能是600个字 } //console.log($('#num_txt').html(str)); }); })
Then introduce multiple textarea implementation cases on the same page
function changeLength(obj,num){ obj.on('keyup',function(){ var txtval = obj.val().length; //console.log(txtval); var str = parseInt(600-txtval); //console.log(str); if(str > 0 ){ num.html('剩余可输入'+str+'字'); }else { num.html('剩余可输入0字'); obj.val(obj.val().substring(0, 600)); } //console.log($('#num_txt').html(str)); }); } $(function(){ //我这里有四个,所以调用4次 changeLength($('#text_txt1'),$('#num_txt1')); changeLength($('#text_txt2'),$('#num_txt2')); changeLength($('#text_txt3'),$('#num_txt3')); changeLength($('#text_txt4'),$('#num_txt4')); });
Of course, the actual number of words required here can also be encapsulated inside the function, but I will not encapsulate it. In this way, when text is entered, the remaining word count will be automatically displayed inside the span. When the input value reaches the maximum value, the remaining word count will be displayed as 0, and new content cannot be filled in. When deletes text, span can dynamically obtain the remaining number of words.
The following is other people’s code. This time I also borrowed some other people’s writing methods
html:
<div class="family_v2"> <p class="nickname_v2">简介:</p> <textarea id="content" name="sign" style="height:60px;overflow-y: hidden;" onkeyup="changeLength(this,60)" class="nicknameBox_v2 brief_box_v2"> </textarea> <div class="limit_num_v2"> <h3 id="">60</h3> </div> </div>
js:
//验证textarea的长度 function changeLength(obj,lg){ var len = $(obj).val(); $(obj).next().find("h3").text(lg-len.length); if(len.length>=lg){ $(obj).next().find("h3").text(0); $(obj).val(len.substring(0,lg)); } }
I believe that I have seen these cases. You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
Basic knowledge of HTML. Detailed introduction to css style sheets and style attributes
of HTML Introduction to common usage of meta tag
The href attribute of HTMLa tag specifies relative path and absolute path usage
The above is the detailed content of How to obtain the dynamic remaining word count of textarea. For more information, please follow other related articles on the PHP Chinese website!

HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
