473,382 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Word count in multiple textarea

Hi

I have several textarea in the same page. I would like to count the
words in each textarea without any interaction with each other. Here's
the code I used for the count :

<script type="text/javascript">

/*Téléchargé sur Easy-Script.com (http://www.easy-script.com)
Trouvé sur: www.portugal-tchat.com */

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxl ength)
return false
else if
(e.target&&e.target==eval(placeholder)&&e.target.v alue.length>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/
if (pressedkey.test(String.fromCharCode(e.which)))
e.stopPropagation()
}
}

function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] :
document.getElementById(placeholder)
if (window.event||e.target&&e.target==eval(placeholde r)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}

function displaylimit(thename, theid, thelimit){
var theform=theid!=""? document.getElementById(theid) : thename
var limit_text='<b><span
id="'+theform.toString()+'">'+thelimit+'</span></b> caractères
maximum.'
if (document.all||ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return
restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) {
restrictinput(thelimit,event,theform) }, true);
document.body.addEventListener('keyup', function(event) {
countlimit(thelimit,event,theform) }, true);
}
}
</script>
This code works if there's only one textarea. If there's more than one,
the word count change in the first text area, not the second... How can
I fix that?

Here's a example :

<form name=form>
<textarea name="description" id="esp" rows="4" cols="22"
style="border: 1px double #B8C8FE; background-color: #0069B3; color:
#FFCC00;"></textarea>
<br>
<font face="verdana" size="2"
color="#B8C8FE"><script>displaylimit("","esp",250) </script><br></font>
<textarea name="description1" id="esp1" rows="4" cols="22"
style="border: 1px double #B8C8FE; background-color: #0069B3; color:
#FFCC00;"></textarea>
<br>
<font face="verdana" size="2"
color="#B8C8FE"><script>displaylimit("","esp1",250 )</script><br></font>
</form>

As you can see, the id is different. But when I write words in the
second textarea, the text change in the 1st text area, not the second.
I'm completely lost because i've never learnt used Javascript... Can
anyone help plz?

Thanks

Mar 17 '06 #1
1 2750
tr********@gmail.com wrote:
I have several textarea in the same page. I would like to count the
words in each textarea without any interaction with each other. Here's
the code I used for the count :

<script type="text/javascript">


Everything below this line is complete junk. You better rewrite
it, starting with a solution to your problem:

function countWords(s)
{
return s.match(/\b\w+\b/).length;
}

Note that if you use \b and \w+, that does not cover the various
ISO-8859-x/UCS word characters. (There is a nice definition in
the XML 1.1 Specification for name characters that you can make
use of.)
PointedEars
Mar 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: reneecccwest | last post by:
Hello, can anybody share the code for a word counter for the textarea user input? what if max characters are 256? thanks
2
by: David | last post by:
After getting some help working through my bugs, I have what seems to be a robust, working word counter script. I post it here to benefit others that might want this in the future and so that if I...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
6
by: Eric Layman | last post by:
Hi, I have fields from textareas. With a click of a button, php is able to grab these fields and by using header(), convert the output to Ms Word doc. But the outcome of the word doc...
4
by: FunkHouse9 | last post by:
I built a form that is depentent on a word count a word count. What I have works just fine in IE but doesn't work in Firefox. It changes the count from 0 to 1 and that's as far as it goes. Here's...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
7
by: Anz | last post by:
Is there any javascript function for counting the number of lines inside a text area ?
1
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.