Sorting Names in an Array using Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    Sorting Names in an Array using Function

    Okay, I have two files one is the HTML where the user inputs a name, and clicks Add. The add button calls on a JavaScript Function created that is suppose to put sort the names. Adding One or more names does not show in the textarea created. Below are the code of the two files...can anyone suggest why it is not working or tell me where I did something wrong?
    HTML Code
    Code:
    <html>
    <head><title>Array Sorting<title>
    <script language="JavaScript" type="text/javascript" src="Sort_Array_Example.js"></script>
    </head>
    <body>
    <h1>Sorting Through Names</h1>
    <p>Enter two or more names and the Sort </br>
    Button will sort the names in the text area</p>
    <form name="the_form">
    Name: <input type="text" name = "new_name" size = "30">
    <input type = "button" name = "add_name" value = "Add" onClick = "SortNames();">
    <br>
    <h2>Sorted Names</h2>
    <textarea cols = "80" rows = "20" name = "namesSorted">
    The sorted names appear here.
    </textarea>
    </form>
    </body>
    </html>
    JavaScript File called Sort_Array_Exam ple.js
    Code:
    var numberNames=0;
    var names = new Array();
    function SortNames(){
    	thename=document.the_form.new_name.value;
    	names[numberNames]=thename;
    	numberNames++;
    	names.sort();
    	document.theform.namesSorted.value = names.join("\n");}
    Last edited by Dormilich; Jan 5 '11, 10:14 PM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s because your array is empty. when you add names you only extend the object, since a JavaScript Array is always numeric.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      You don't have an element named theform. You have one named the_form though.

      Comment

      • Brian Connelly
        New Member
        • Jan 2011
        • 103

        #4
        Thanks for the help everyone. The solution was to change the document.thefor m... to document.the_fo rm... Thanks for all the insight. As I learn, it is great to have persons like you to help guide me through the darkness!

        Comment

        Working...