Forum Moderators: open

Message Too Old, No Replies

Counting characters after stripping html

Javascript to count the characters of a textarea excluding tags

         

t2dman

9:31 am on Aug 8, 2004 (gmt 0)

10+ Year Member



I can successfully count the characters in a textbox, but I want to count the characters excluding html tags

<textarea name="firstwords"
onKeyDown="textCounter(document.metas.firstwords,document.metas.remLen2)"
onKeyUp="textCounter(document.metas.firstwords,document.metas.remLen2)">
</textarea>
<input readonly type="text" name="remLen2" size="3" maxlength="3">

I have also called the function in body onload

javascript
function textCounter(field,cntfield) {
cntfield.value = field.value.length;
}

I want to somehow use the following to get
var strip = "";
strip = field.replace(/<&#91;^>&#93;*>/g, "");
cntfield.value = strp.length;

I am rather new to Javascript syntax. Your help would be appreciated.

BjarneDM

8:55 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



I can't see anything wrong with your search string except for the fact that you are using html entities instead of the literal characters for [ and ] so the search string simply ought to be : /<[^>]*>/g

and - of course - a spelling error in strp.length ;-)

t2dman

9:48 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



The following returns an error

function textCounter(field,cntfield) {

var strip="";
strip = field.replace(/<[^>]*>/g, "");
cntfield.value = strip.length;

}

error=object doesn't support this property or method refering to the -> cntfield.value = strip.length;

I have tried with cntfield.value = strip.value.length;

Mixing a variable, with a form field = issues

BjarneDM

12:55 am on Aug 10, 2004 (gmt 0)

10+ Year Member



try :
cntfield.value = strip.length.toString;
the issue being that all form fields are text.

if that doesn't work, then I'ld like to see how cntfield is defined in the html

t2dman

7:09 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



function textCounter(field,cntfield) {

var strip="";
strip = field.replace(/<[^>]*>/g, "");
cntfield.value = strip.length.toString;
}

same error for the cntfield.value line - object doesn't support this method. Thanks for your help, but we havn't got there yet.

<form method="post" action="admin.php" name="metas">

<textarea name="firstwords" onKeyDown="textCounter(document.metas.firstwords,document.metas.remLen2)"
onKeyUp="textCounter(document.metas.firstwords,document.metas.remLen2)"></textarea>

<input readonly type="text" name="remLen2" size="3" maxlength="3">
</form>

BjarneDM

9:24 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



put id attributes into your form elements too.

that is: <name="remLen2" id="remLen2"> and <name="firstwords" id="firstwords">

problem:
when submitting a form the name attribute is used
when referencing form elements in a javascript the id attribute is used
thus, you need to define both an id and a name attribute for all form elements

t2dman

11:40 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



Good idea, but still not working

BjarneDM

7:28 am on Aug 12, 2004 (gmt 0)

10+ Year Member



All right - we need to debug your script.

First order of business is to find out what's getting into the script. Insert these lines as the first two in you javascript:
alert(field);
alert(cntfield);
and see if they have got the values you expect.

Another thing to try is to assign a value attribute to your remLen2 in the html code

t2dman

10:09 am on Aug 12, 2004 (gmt 0)

10+ Year Member



It now works - thanks. Needed the value in field.value.replace

function textCounter(field,cntfield) {
strip = field.value.replace(/<[^>]*>/g, "");
cntfield.value = strip.length;
}