473,801 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ge tElementById&&! document.all

function restrictinput(m axlength,e,plac eholder){
if (window.event&& event.srcElemen t.value.length> =maxlength)
return false
else if
(e.target&&e.ta rget==eval(plac eholder)&&e.tar get.value.lengt h>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/
if (pressedkey.tes t(String.fromCh arCode(e.which) ))
e.stopPropagati on()
}
}

function countlimit(maxl ength,e,placeho lder){
var theform=eval(pl aceholder)
var lengthleft=maxl ength-theform.value.l ength
var placeholderobj= document.all? document.all[placeholder] :
document.getEle mentById(placeh older)
if (window.event|| e.target&&e.tar get==eval(place holder)){
if (lengthleft<0)
theform.value=t heform.value.su bstring(0,maxle ngth)
placeholderobj. innerHTML=lengt hleft
}
}

function displaylimit(th ename, theid, thelimit){
var theform=theid!= ""? document.getEle mentById(theid) : thename
var limit_text='<b> <span
id="'+theform.t oString()+'">'+ thelimit+'</span></b> caractères
maximum.'
if (document.all|| ns6)
document.write( limit_text)
if (document.all){
eval(theform).o nkeypress=funct ion(){ return
restrictinput(t helimit,event,t heform)}
eval(theform).o nkeyup=function (){ countlimit(thel imit,event,thef orm)}
}
else if (ns6){
document.body.a ddEventListener ('keypress', function(event) {
restrictinput(t helimit,event,t heform) }, true);
document.body.a ddEventListener ('keyup', function(event) {
countlimit(thel imit,event,thef orm) }, 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="descripti on" 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>displa ylimit("","esp" ,250)</script><br></font>
<textarea name="descripti on1" 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>displa ylimit("","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 2773
tr********@gmai l.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
3204
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
1602
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 ever lose my copy I can come back here to find it :) Some other scripts that I used for inspiration failed when confronted with whitespace before the string or miscalculated when encountering linefeeds and other non-space spaces, so I made mine...
7
6253
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 user and populated (with info from an Access database) at run-time, then saved as Word documents. The program I have coded works fine -- it does what I need it to do. But it has two problems: (1) it runs very slowly, and (2) it does not seem to...
4
12452
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
6
4645
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 doesn't wrap the text in textarea. the result is one very veyr long string of text which stretch MS word all
4
2540
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 what I'm doing. <script type=text/javaScript> <!-- function word_count(val) { val = val.split(/\s+/g); document.form.op3.value = val.length; }
6
3392
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 only and place square brackets around them e.g word = car PASSAGE: the red car required a lot of care to prevent a scar I only want it to place a square bracket around the word car and ignore the car in "Care" and "scar"
7
17634
by: Anz | last post by:
Is there any javascript function for counting the number of lines inside a text area ?
1
3210
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 code for attachment, I am not able to send anything. I get blank email. Can you please help me on this? Here is the html form:
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10298
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9105
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2963
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.