...

View Full Version : Add the characters in the fields



lipak
06-26-2004, 05:11 AM
Here is the character count script I am working with:
http://codeline-alley.digiserv.net/javascript/textarea_character_count.php

I don't know how to use it so that the character count for multiple fields (text areas) is totaled and displayed in the "characters left" field.

Any help would be greatly appreciated.

Willy Duitt
06-26-2004, 05:25 PM
Is this (http://www.txt.bellmobility.ca/bmc/en/) the page you are applying the code too?

If so, I just checked at it appears to be working.
At least for two fields anyway....

.....Willy

lipak
06-26-2004, 06:20 PM
Thanks for the reply. I found cross browser compatible code on this forum so I thought I would repost. That page you linked to is a page that does what I want to do, but it is not my page. I'm where I was before - minus the cross browser issue.

neofibril
06-26-2004, 06:48 PM
Well, I don't know what's going on with the replies above, but in response to the original question, this might work:


<body>
<script type="text/javascript">
function doCount(elem, count, limit, names)
{
names = names.split("+");
var pForm = elem.form,
total = 0,
val = elem.value,
ln = names.length,
i = -1;
while(++i < ln)
total += pForm[names[i]].value.length;
total > limit ? elem.value = val.substr(0, val.length - 1) : count.value = limit - total;
}
</script>
<form>
<fieldset style="text-align:center;padding-bottom:1em">
<legend>
<label>characters remaining: </label>
<input name="counter" value="20" size="4" readonly>
</legend>
<textarea name="txt1" cols="20" rows="2"
onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
<textarea name="txt2" cols="20" rows="2"
onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
<textarea name="txt3" cols="20" rows="2"
onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
</fieldset>
</form>
</body>


Edit:
I hadn't tested that in Opera: a browser that requires an additional onkeypress handler...
it also added my linebreaks to the textarea values (those have been removed). :D

I suppose all of the parameters should be put into the function as variables, given the number of event handlers that they'd be stuffed into.

lipak
06-27-2004, 07:50 PM
Sorry for the delayed reply. It works perfectly! Thank you very much neofibril and Willy Duitt for all your help. It is greatly appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum