Mikeyham Posted March 29, 2009 Share Posted March 29, 2009 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 More sharing options...
Ingolme Posted March 29, 2009 Share Posted March 29, 2009 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 More sharing options...
Mikeyham Posted March 29, 2009 Author Share Posted March 29, 2009 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 More sharing options...
Ingolme Posted March 29, 2009 Share Posted March 29, 2009 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 More sharing options...
Mikeyham Posted March 29, 2009 Author Share Posted March 29, 2009 Yes. It should be just a string.Alright. That makes sense, thanks ingolme! =D Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.