This code originally worked, but now it doesn't. It's suppose to count the number of characters you type. The form processes fine, but it's not counting, it always says 1,000 in the box. Any ideas? Thanks
Code:
<script type = "text/javascript">
var maxLen = 1000; // max number of characters allowed // global variable
var max = "You may enter up to " + maxLen + " characters"; // global variable
function OnPaste () {
//return false; // cancels (blocks) the onpaste event. Uncomment this line to block pasting
setTimeout(checkMaxInput,100); // delay is necessary
}
function initCount() {
document.getElementById("limit").innerHTML = max;
document.getElementById("remLen").value = maxLen;
}
function checkMaxInput() {
var form = document.myform; // or document.forms[0] or document.getElementById("myform");
if (form.txtarea.value.length > maxLen) { // if too long.... trim it!
form.txtarea.value = form.txtarea.value.substring(0, maxLen);
document.getElementById("message").innerHTML = "Too many characters were entered!! The excess over " + maxLen + " have been removed.";
}
else {
document.getElementById("message").innerHTML = "";
}
form.remLen.value = maxLen - form.txtarea.value.length;
}
</script>
<body onload = "initCount()">
<form name = "myform" id = "myform">
<span id = "limit" style="font-size: 10pt;color: #FF0000;font-family: arial, helvetica, sans-serif;"></span>
<br>
<textarea name="txtarea" wrap=physical cols=48 rows=10 onkeyup="checkMaxInput()" onblur="checkMaxInput()" onpaste="OnPaste ()" >
</textarea>
<br>
<input readonly type=text name=remLen id = "remLen" size=3 value = ""> characters left</font>
</form>
<span id = "message" style="color:red";></span>
__________________
Been a sign maker for 5 years. My business: American Made Signs
If you need something that will run on page load, just add the following to the body tag:
onLoad="textCounter(document.forms['thisForm'].message,document.forms['thisForm'].remLen,1000);"
Works fine in Firefox, with or without Firebug running.
Excepting only that if you CTRL-V to paste in the last batch of characters it doesn't show the "too many" message. But it seems to chop it fine at 1000.
I think because the onpaste fires, which shows the message, but then the onkeyup fires and sees there are exactly 1000 characters and says "gee, everything is fine".
And indeed "OK" gets appended to the "too many" message after a paste.
If it's not working for you, show the URL of the page with the problem. I'll bet that some other JS code is interfering. Your <body onload="...">, for example, could be getting wiped out.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Incidentally, the reason document.forms[0] wouldn't work is because *THAT* refers to your <form> that starts with
Code:
<form method="get" action="">
That is, your search form at the top of the page.
And, by the way, if you expected to submit the textarea contents to your freeposts.php page, that wouldn't happen, either, since it is not a legal part of the form with that action and is instead in the illegally nested <form>.
*********
Wolfshade beat me but only because I'm so long-winded. <grin/>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Stop being such a jerk about it. I am trying here.
myfayt - you will not win friends and influence people if you insult experienced people who are trying to help you. Old Pedant has explained very clearly what is wrong with you code. You ought to thank him, not abuse him. How about apologising?
"Thou callest me a dog before thou hast cause. But since I am a dog, beware my fangs. " -William Shakespeare, The Merchant of Venice
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Works fine in Firefox, with or without Firebug running.
Excepting only that if you CTRL-V to paste in the last batch of characters it doesn't show the "too many" message. But it seems to chop it fine at 1000.
I think because the onpaste fires, which shows the message, but then the onkeyup fires and sees there are exactly 1000 characters and says "gee, everything is fine".
That seems to be solved by changing this line to:-
if (form.txtarea.value.length >= maxLen) { // if too long.... trim it!
That also overcomes a stange bug where if the user clicks on the error message it disappears.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
myfayt - you will not win friends and influence people if you insult experienced people who are trying to help you. Old Pedant has explained very clearly what is wrong with you code. You ought to thank him, not abuse him. How about apologising?
"Thou callest me a dog before thou hast cause. But since I am a dog, beware my fangs. " -William Shakespeare, The Merchant of Venice
No if you had read it before he deleted it, it was really bad, he was being immature about it and getting a nasty attitude with me simply because I am not as experienced as the rest of you.
It takes me longer to learn things. But instead of being patient, I get yelled at for not understanding it on the first go.
I will just find someone else for help, since clearly everyone is against me in this thread.
__________________
Been a sign maker for 5 years. My business: American Made Signs
No if you had read it before he deleted it, it was really bad, he was being immature about it and getting a nasty attitude with me simply because I am not as experienced as the rest of you.
It takes me longer to learn things. But instead of being patient, I get yelled at for not understanding it on the first go.
I will just find someone else for help, since clearly everyone is against me in this thread.
I did see Old Pedant's response, and although he used capitals for emphasis I don't think he was shouting at you or had anything which could be described as a "nasty attitude". In fact in my experience Old Pedant shows extraordinary patience with people who are a little slow on the uptake - sorry, those who take longer to learn things.
It is never a good idea to insult people who are trying to help you. You could have expressed yourself in a more adult way. Nor is it an adult reaction to assert that "everyone is against me in this thread". The outcome is, as you say, that you will now have to look for help elsewhere. Who's loss is that?
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.