Hello all,
I’m only a few lessons into learning JavaScript, which is probably why I can’t figure out what I’m doing wrong here…
I pasted the following code into my html page:
[COLOR=red]in the <head>:
<script type=”text/javascript” language=”javascript” src=”sort.js”>
</script>
[COLOR=red]in the <body>:
<form name=”theform”>
Name:
<input type=”text” name=”newmame” size=”40″ />
<input type=”button” name=”addname” value=”Add” onlick=”SortNames();” />
<br />
<h3>Sorted Names</h3>
<textarea cols=”60″ rows=”10″ name=”sorted”>
The sorted names will appear here.
</textarea>
</form>
[COLOR=red]in a separate file named sort.js:
// 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 names to the array
names[numnames]=thename;
// Increment the counter
numnames++;
// Sort the array
names.sort();
document.theform.sorted.value=names.join(“n”);
}
[COLOR=red]What’s supposed to happen:
When you input two or more names, then click ‘Add’, the names are supposed to be sorted alphabetically in the textarea below.
[SIZE=4][COLOR=red]My Problem:
I open this page in my browser (IE7 and Firefox), and when I click the “Add” button, nothing happens. This script is straight out of a JavaScript teaching book, so I am extremely confident that the script works, I just think that because I am a complete newb, I am missing something simple (and perhaps obvious to one of you.)
If it matters – I have not uploaded this HTML page to the web. I am opening the file from my own computer.