JavaScript - IE6 (SP1) throws errors on Microsoft's own page!
: > The reason is do***ent.all is trying to reference the element by ID,
: > not name. So, to make it work, you need to add an ID to the SPAN.
:
:
: That's it! Here's the original form code: :
: <td>
: <textarea cols="50" rows="5" style="width:420px;"
: onKeyUp="countText()" id="commentArea" name="sFeedbackComment"></textarea>
: </td>
:
: And the offending line from function CountText():
:
: do***ent.all("characterCount").innerText = commentTextLength;
: Should be:
: do***ent.all("commentArea").innerText = commentTextLength;
:
: The error was "do***ent.all("...") is null or not an object. So it's just
a
: programming error, not something wrong with my setup. Thanks a lot for
this
: impressive detective work. I note your comment about including the form
: code - I'll know next time.
:
:
: There are 837 lines (mostly JS) on what looks like a simple form! The
code
: is dynamically generated, as there are order-specific details in there.
I believe that might be incorrect. The part of the form or outside the form
that actually gets the value of the character count is what is missing the
ID. I don't think the script is in error. commentArea is the text in the
TEXTAREA. If you assign a value to the innerText of the TEXTAREA, then you
overwrite your input.
I noticed you cross-posted, which is good but you also multi-posted to the
jscript forum. It's not good to multi-post and you'll get the wrath but you
answered both so just as well except better to include the whole discussion
together. I'm just referencing and not pointing fingers as I'm sure it was
an after thought to include the other forum.
In the jscript response you got, Martin said it referenced ID or NAME but
when I tried to reference do***ent.all by name, it didn't work. I'm
wondering what MY error was?! I'll add the jscript forum to this discussion
to see if Martin or someone can elighten us. Note: I thought the same as
Martin until I tested it but I hardly ever, if at all, use the do***ent.all
collection. I prefer to use getElementById, which requires a more updated
browser.
I'm adding part of what I posted so the jscript group can see what I'm
referring to.
Thanks for following up. (O:=
[from earlier response]
The error appears to be on the form.
Ex. (this will error out and point to the above line as you experienced)
<div><span name="characterCount"></span><span> characters</div>
<form name=frmFeedback>
<textarea name=commentArea rows=5 cols=50 onkeyup="countText()"></textarea>
</form>
The reason is do***ent.all is trying to reference the element by ID, not
name. So, to make it work, you need to add an ID to the SPAN.
<div><span id=characterCount
name="characterCount"></span><span> characters</div>
<form name=frmFeedback>
<textarea name=commentArea rows=5 cols=50 onkeyup="countText()"></textarea>
</form>
Here is a full working model:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT type="text/javascript" LANGUAGE="javascript">
<!--
//---------------------------------------------------------
// this function counts up the text in the comment area and
// displays it on the page
//---------------------------------------------------------
function countText()
{
var commentText = do***ent.forms.frmFeedback.commentArea.value;
var commentTextLength = 0;
commentTextLength = commentText.length;
//### marker comment added by OP - see next line ### do***ent.all("characterCount").innerText = commentTextLength;
if (commentTextLength > 1000)
{
alert('Please limit your comments to 1000 characters.');
do***ent.forms.frmFeedback.commentArea.value =
commentText.substring(0,1000);
countText();
}
return true; }
</script>
</HEAD>
<BODY>
<div><span id=characterCount
name="characterCount"></span><span> characters</div>
<form name=frmFeedback>
<textarea name=commentArea rows=5 cols=50 onkeyup="countText()"></textarea>
</form>
</BODY>
</HTML>
[eor]
When id=characterCount is removed from the SPAN, the counter doesn't work.
Proof of concept:
http://rockintheplanet.com/lab/allwithoutid.html
http://rockintheplanet.com/lab/allwithid.html
--
Roland
This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose.
-Technet Script Center-
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/default.asp
-MSDN Library-
http://msdn.microsoft.com/library/default.asp
|