Page 1 of 1

javascript character count not working

Posted: Sat Aug 02, 2003 3:30 pm
by m3rajk
is this me using the wrong functions or is it something else causing this not to work... whille the code is prepped for a php file, it's actually from an html file i whipped up to get the javascript right so i know any future issues are either with php or the db

note: i'd prefer to get the first working

Code: Select all

<html><head></head><body><center>
<form action="<?php echo $_SERVER&#1111;PHP_SELF]; ?>" name="new_friend" method="POST" onReset="window.close();">
<table frame="void" bgcolor="#000000" border="0" cellpadding="0" cellspacing="0" text="#c8c8c8">
<tr>
<td>Selected Friend</td>
<td><input type="text" name="fun" readonly size="15" value="<?php echo $_GET&#1111;'un']; ?>"></td>
</tr>
<tr>
<td colspan="2">
<textarea cols="30" name="cof" onKeyUp="return findlength();" rows="4" wrap="virtual">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above that will be lost
</textarea>
</td>
</tr>
<tr><td colspan="2">Characters in Comment:<input type="text" name="chars" readonly size="3" value="0"></td>></tr>
<tr>
<td><input type="submit" value="Add My Friend!"></td>
<td><input type="reset" value="Not My Friend"></td>
</tr>
</table>
</form>
<script language="javascript">
function findlength()&#123;
var comment=document.new_friend.cof.value(); // get the comment
var numchars=comment.length; // find the number of characters
document.new_friend.chars.write(numchars); // let the user know the number of characters
&#125;
</script>
</center>
</body>
</html>
and the variation i made thinking maybe i could moorph it a bit to look like something else i made that does work....

Code: Select all

<html><head></head><body><center>
<form action="<?php echo $_SERVER&#1111;PHP_SELF]; ?>" name="new_friend" method="POST" onReset="window.close();">
<table frame="void" bgcolor="#000000" border="0" cellpadding="0" cellspacing="0" text="#c8c8c8">
<tr>
<td>Selected Friend</td>
<td><input type="text" name="fun" readonly size="15" value="<?php echo $_GET&#1111;'un']; ?>"></td>
</tr>
<tr>
<td colspan="2">
<textarea cols="30" name="cof" onKeyUp="return findlength();" rows="4" wrap="virtual">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above that will be lost
</textarea>
</td>
</tr>
<tr><td colspan="2"><iframe name="chars" src="chars.html"></iframe></td></tr>
<tr>
<td><input type="submit" value="Add My Friend!"></td>
<td><input type="reset" value="Not My Friend"></td>
</tr>
</table>
</form>
<script language="javascript">
function findlength()&#123;
var comment=document.new_friend.cof.value(); // get the comment
var numchars=comment.length; // find the number of characters used
parent.chars.document.open();
parent.chars.document.write("<html><body>Your comment is "+numchars+" long.</body></html>");  // tell the user the number of character used
parent.chars.document.close();
&#125;
</script>
</center>
</body>
</html>

Posted: Sat Aug 02, 2003 6:46 pm
by patrikG
In function findlength() replace

Code: Select all

var comment=document.new_friend.cof.value();
with

Code: Select all

var comment=document.new_friend.cof.value;
as value can't be a method or function, only a property.

and replace

Code: Select all

document.new_friend.chars.write(numchars);
with

Code: Select all

document.new_friend.chars.value=numchars;
because you are assigning a value to a form-field, you are not writing to the document.

The entire code (your first sample) with the ammendments above:

Code: Select all

<html><head></head><body><center>
<form action="<?php echo $_SERVER&#1111;PHP_SELF]; ?>" name="new_friend" method="POST" onReset="window.close();">
<table frame="void" bgcolor="#000000" border="0" cellpadding="0" cellspacing="0" text="#c8c8c8">
<tr>
<td>Selected Friend</td>
<td><input type="text" name="fun" readonly size="15" value="<?php echo $_GET&#1111;'un']; ?>"></td>
</tr>
<tr>
<td colspan="2">
<textarea cols="30" name="cof" onKeyUp="return findlength();" rows="4" wrap="virtual">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above that will be lost
</textarea>
</td>
</tr>
<tr><td colspan="2">Characters in Comment:<input type="text" name="chars" readonly size="3" value="0"></td>></tr>
<tr>
<td><input type="submit" value="Add My Friend!"></td>
<td><input type="reset" value="Not My Friend"></td>
</tr>
</table>
</form>
<script language="javascript">
function findlength()&#123;
var comment=document.new_friend.cof.value; // get the comment
var numchars=comment.length; // find the number of characters
document.new_friend.chars.value=numchars; // let the user know the number of characters
&#125;
</script>
</center>
</body>
</html>

Posted: Sun Aug 03, 2003 11:30 am
by m3rajk
danke
works perfectly now, and better than taht, i know why it was screwing up

i have this code form something else the second one is based on and that was writing frames, i guess that's why i thought to write. and the other thing i thought was a function to get the value. this is exactly why i hate it when people just give the code, i would only have known that there's no () on it, not what was actually helping, therefore my understanding of javascript would not have improved.

once again, thank you