The question "How can I obtain input field numbers?" can be posed as a two-part question:
document" object. This object has many properties, some of which may be objects corresponding to forms displayed in the document. Form objects, in turn, also have properties, some of which may be objects corresponding to input fields in the form. As you might expect, input fields also have properties. One of these properties is a string object called "
To illustrate this better, let's consider the following code:
On your browser, this code produces a document with the following appearance:
The value of the input field in this document can be accessed through the object called "
document". Notice that in the document is a form, which has carefully been given the name, "
apple". Because this form has a name, we can directly refer to the form object as a property of the document object, namely, "
The text field in the form also has a name, "
banana". Through this name, we can refer to the text field object as a property of the form object, namely, "
Finally, the text field has an object property called "value", which is a string object.
Thus, the value of the text field can be accessed through the object-property path "
this.value", or even just "
value", can also be used to mean the same thing.
valueproperty of an input field object is a string. Thus, in order to treat
valuelike a number, it must first be converted to an integer through the
parseInt, in its simplest form, accepts a string as input and returns the base-10 number represented by the string. Thus, in the sample code given above, the value of the input field is obtained through the expression
var temp = parseInt (document . apple . banana . value);
parseIntfails when the string does not contain digits that can be converted into a number. When this happens, it returns a special value called "NaN", which means "Not a Number." Thus, whenever
parseIntis used to convert a string to a number, a special check should be made to determine whether the value returned is a legitimate number or a NaN.
isNaN that serves this purpose.
isNaN accepts a single parameter of any type and returns the boolean value
true if the parameter is a NaN,
false otherwise. In the above example, we check the value obtained from
parseInt with the following statement:
This makes it possible for our code to avoid performing arithmetic on non-numeric strings.
if (isNaN (temp)) return;