View Full Version : could someone explain...
chrismiceli
09-11-2002, 01:52 AM
could someone explain the parseInt() command for me plz?
Hi chrismiceli,
http://developer.netscape.com/docs/manuals/communicator/jsref/glob15.htm#1012508
( •) (• )
>>V
chrismiceli
09-11-2002, 02:24 AM
thanx I get everything but in this code why is there a [0]?
<html>
<head>
<title>Converter</title>
<script language="javascript">
function usql(mult){
answer = parseInt(mult) * 1.06;
document.forms[0].result.value = answer ;
}
</script>
</head>
<body>
<form>
<input type="text" name="number" value="">
<input type="text" name="result" value="">
<p>
<input type="button" name="ql" value="Quart to Liter" onClick="usql(this.form.number.value)">
</form>
</body>
</html>
beetle
09-11-2002, 04:59 AM
document.forms is what is commonly referred to as a collection (or, array) meaning that it has multiple pieces of data or objects within it. We normally access these pieces with a numerical index. Arrays are zero-based, meaning that the first element in an array as the index of 0. So, document.forms[0] is a reference to the first form (in source order) in the document.<form id="form1">
<!-- inputs -->
</form>
<form id="form2">
<!-- inputs -->
</form>in the HTML above, document.forms[0] is a reference to 'form1', because it is the first form in the document. document.forms[1] is a reference to 'form2'. document.forms[2] is an invalid reference in this example since there is no 3rd form.
Let's take the following array as an example
var grades = new Array(90, 87, 65, 95);
Now, it's important to know that an array stores two pieces of data per entry. It's obvious that it stores the data you enter (in this case, the grades) but it also stores the index for that data. +----+----+----+----+
| 0 | 1 | 2 | 3 | <-- Indexes, or pointer
+----+----+----+----+
| 90 | 87 | 65 | 95 | <-- values
+----+----+----+----+So in the example above, grades[2] = 65 Got that?
Now, why did the script you post use document.forms[0]?? Well, because that form object has not been assigned an ID (unlike my example above), so it must be refereced with the forms collection of the document object. Since it is the first and only form on the page, the index of 0 was used.
Ok, my fingers are tired now....:D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.