What’s this stuff mean?
The Javascript File Code:
// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
thename=document.theform.newname.value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
// Sort the array
names.sort();
document.theform.sorted.value=names.join(“\n”);
}
The HTML Code:
<html>
<head>
<title>Array Sorting Example</title>
<script type=”text/javascript” language=”javascript” src=”sort.js”> <<This part links to the Javascript file above
</script>
</head>
<body>
<h1>Sorting String Arrays</h1>
<p>Enter two or more names in the field below,
and the sorted list of names will appear in the
text area.</p>
<form name=”theform”> <<Here’s where the form is first identified so that the statement “document.theform…,” in the function SortNames, knows where to go and grab something.
Name:
<input type=”text” name=”newname” size=”20″> <<This is the place the value “newname” is entered by the user and located by the statment “thename=document.theform.newname.value;” in the function SortNames.
<input type=”button” name=”addname” value=”Add”
onclick=”SortNames();”> <<This is where the function SortNames is activated by user’s click.
<br>
<h2>Sorted Names</h2>
<textarea cols=”60″ rows=”10″ name=”sorted”> <<Here, the text area “sorted” is identified, so the method “names.sort” knows where to put the data identified by the property below it: “document.theform.sorted.value=names.join(“\n”);”
The sorted names will appear here.
</textarea>
</form>
</body>
</html>
Advertisement
leave a comment