[thelist] Textarea Javascript

Andrew Clover and at doxdesk.com
Fri Apr 26 15:03:01 CDT 2002


Josh S Feingold <Josh.S.Feingold at irs.gov> wrote:

> I noticed that this:

>       textarea = document.form.textarea.value

> does not work

No. If you have an form element with name 'textarea', many browsers will
put it in global scope for you. This means you can write a shortcut like:

  t= textarea.value;

This is one of the many idiotic features which was added to JavaScript to
make it "easier" for new coders, but actually has the effect of making the
language far more confusing for everybody. In your line above, you're
reading the value of the textarea, and then assigning it to the object
in global scope with the name 'textarea' - which is the form field object
you just read from. Since you can't assign directly to a form field object,
an error is caused.

Declare all your new variables with 'var', so it knows you're talking about
a variable and not some other nonsense the browser has "helpfully" decided
to shove into global scope for you.

And it generally helps to avoid shortcuts. eg.:

  var t= document.forms['form'].elements['textarea'].value;

is best. But you'll still be screwed by JavaScript's namespace tomfoolery
if, for example, you have an input with name="elements".

JS is really really poorly designed.

--
Andrew Clover
mailto:and at doxdesk.com
http://and.doxdesk.com/



More information about the thelist mailing list