DEV Community

Nonny123
Nonny123

Posted on

Word count using jQuery

This post is aimed at creating a jQuery word count function. The code below can be used to limit the total number of words in a textbox (e.g Abstract for research thesis report) using jQuery.

function WordCount() { //Get Text data var inp = document.getElementById('txtabstract').value; var regex = /\s+/gi; var wordcount = jQuery.trim(inp).replace(regex, ' ').split(' ').length; $("#labelcount").html(wordcount); } $(document).ready(function(){ //count word on page load WordCount(); //count word on entering text in TextBox $("#txtabstract").on('input',function(){ WordCount(); }) })

This code is used to let a user know when the abstract for his project report has exceeded 300 words (which is the standard for an abstract) at Classgist

Latest comments (3)

Collapse
 
beconismylife profile image
ฉันเกิดมากินเบค่อนและดับไป

1 ตรวจเช็ค Stabilizer ต้องอยู่ที่ 200 V
2 ตรวจเช็คลมต้องอยู่ที่ 0.23-0.25 Mpa (Buhler) 0.4 Mpa
3 ตรวจเช็คชุดตัวทำความสะอาดกระจก
4 ตรวจเช็คลมต้องไม่รั่วออกจากหัวยิง
5 ตรวจเช็ครางลำเลียงข้าวต้องไม่มีรำเกาะ
6 ตรวจเช็คบอร์ดต้องใช้งานได้ปกติ
7 ตรวจเช็คชั่วโมงการทำงานของหลอดไฟ (841,1402 ,1404 = 3000 ชม.,Buhler = 50,000 ชม.)
8 ตรวจเช็คการทำงานและเสียงที่ผิดปกติเวลาใช้งาน
9 ตรวจสอบความสะอาดของเครื่องจักรและพื้นที่โดยรอบบริเวณ
10 ตรวจสอบความสะอาดของถาดสุ่มข้าว
11 ตรวจเช็คน๊อตยึดเครื่องต่างๆ ให้ครบตามจำนวน

Collapse
 
kepta profile image
Kushan Joshi

Hey Okafor, Great read!
I just had few humble suggestions to make this article or any future article even more awesome !

  • if you could the move the hard coded values like txtabstract or labelcount to WordCount parameters, it would make it a lot more reusable and a person like me could simply copy paste and pass the right argument.
  • since it is a browser code and runkit is failing to run it, a new comer reading this article might be confused. I think it would be great to simply use markdown code snippet for it.

  • I would love to read another article which does the same thing with pure vanilla Javascript <3

Collapse
 
nonny123 profile image
Nonny123

Ok. Thanks. Will put these into consideration when creating my next post.