Jump to content

Forms Property Syntax


Mikeyham

Recommended Posts

Greetings. Progressing through the JS bible, and I was messing around with one of the example scripts. It's listed in the book as

function upperMe(){var field = document.forms[0].converter;var upperCaseVersion = field.value.toUpperCase();field.value = upperCaseVersion;}

where converter is text input element. My question is, why couldn't it be done like this:

function upperMe(){var field = document.forms[0].converter.value;var upperCaseVersion = field.toUpperCase();field = upperCaseVersion;}

?The only difference is the former version of "field" represents the reference to the "converter" element, whereas in the latter it represents the value as a string.Edit: I just tested it on Firefox3, but haven't tried it on any others though.

Link to comment
Share on other sites

Because "field" is a reference to the object. In order to put a string into it you have to refer to it.By assigning "document.forms[0].converter.value" to a variable, you are only putting a string into the variable, you are not always referring to the value property of the object (it may depend on the browser).

Link to comment
Share on other sites

I see. So the statement "field = upperCaseVersion;" is really where the rubber meets the road here, because field just represents a string and not the element itself?

Link to comment
Share on other sites

I see. So the statement "field = upperCaseVersion;" is really where the rubber meets the road here, because field just represents a string and not the element itself?
Yes. It should be just a string.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...