Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm putting together this grocery list project and I'm suppose to select a radio button for the grocery product category or department and than enter the item name. Insert both into a text-area. I than need a way to return the values to the text-area sorted alphabetically I'm not concerned about the items being alphabetical just the categories.

This is what I have so far it works for filling in the text-area with the values but the sorting function I can't get to work.
XML
<html>
  <head>
    <title>Grocery List</title>
    <script type="text/javascript" language="JavaScript">
        
	function addItem() 
        {
            var myForm = document.forms.groceryList;
            var listAdd = [];
            for (i = 0; i < myForm.category.length; i++) {
                if (myForm.category[i].checked) {
                    break;
                }
            }
            type = myForm.category[i].value;
     
            var myItem = document.getElementById("itemName").value;
         
            listAdd.push(type + " : "  + myItem + "\n");
           
	    var area = document.getElementById('list');
			
	    area.value = listAdd + area.value;
         }

        function printSortedList()
        {
	
	    newList = document.getElementById('list')
	    newSort = newList.value;
	    newItems = newSort.sort();
	    var tArea = document.getElementById('list');
	    tArea.value = newItems
			
        }
    </script>
    <style type="text/css">
       body,td,th {
	  color: #00F;
       }
       body {
	  background-color: #E4E4E4;
	  background-image: url(grocery_bag.jpg);
       }
    </style>
  </head>
  <body>
    <form action="#" name="groceryList">
      Category: <input type="radio" name="category" value="Produce" />Produce
      <input type="radio" name="category" value="Meat and Cheese" />Meat and Cheese
      <input type="radio" name="category" value="Grocery" />Grocery
      <input type="radio" name="category" value="Beverage" />Beverage
      <br />
      Item: <input name="item" type="text" id="itemName" size="30" />
      <br />
      <input type="button" name="add" value="Add to List"  önclick="addItem()"/>
      <br />
      <textarea name="list" rows="30" id="list" style="width:500" size="10"></textarea>
  
     </select>
  
     <br />  
     <input type="button" id="button" name="sort" value="Print Sorted List"
 önclick="printSortedList()"/>
    </form>
  </body>
</html>
Posted
v2

1 solution

You're trying to sort a string, but "sort" is an array method.

Setting the contents of the textarea from an array works because the array is automatically converted to a string, but you will have to split the string you get back from the textarea into an array before sorting it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900