pa************@gmail.com wrote on 17 feb 2007 in comp.lang.javascript
:
// JavaScript Document
// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
SortNames toUpperCase();
The name of a function cannot return a string in itself,
[unless you programme recursive, which is not here]
toUpperCase() should be attached by a period like this:
var a = 'A StrinG';
alert( a.toUpperCase(); ); // shows: A STRING
//Change the names to all upper case
thename=document.theform.newname.value;
This could work, if the form is correctly named
better keep your variables local, and do this:
var thename =
document.forms['theform'].elements['newname'].value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
You forgot to define a loop, this way only names[0]
is created and assigned
// Sort the array
names.sort();
document.theform.sorted.value=names.join("\n");
you will need to end the function with a }
============
If, Paul, I did not misread your intended purpose,
perhaps the tested code below will help you:
============= test.html ======================
<script type='text/javascript'>
function SortNames() {
var names = new Array();
var f = document.forms['theForm'];
for (var n=0;n<3;n++)
names[n] =
f.elements['elem' + n].value.toUpperCase();
f.elements['sorted'].value =
names.sort().join("+");
return false;
};
</script>
<form name='theForm' onsubmit='return SortNames();'>
<input name='elem0' value='xyz x'><br>
<input name='elem1' value='ABC x'><br>
<input name='elem2' value='Pqr x'><br>
<input type='' name='sorted' value=''><br>
<input type='submit' value='Sort me'><br>
</form>
=================================================
Are you sure I am not making your school assignment?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)