0
<html>
<head>      
</head>
<body>
         
	<script type="text/javascript">
	     
		    var arr = new Array();	
            var sortedARR = new Array();	
            // FORM to input the number of names		
			document.write("<form name=\"TEN\" method=\"\" action=\"\">");
			document.write(" Please enter the numbers of names u want to input:<input type=\"text\" name=\"NoName\"> ");
			var No = document.TEN.NoName.value;
			var N;
			N = eval(No); 
			document.write("<input type=\"button\" name=\"\" onclick=\"INPUT()\" value=\"Click for input!\" >");
			document.write("</form>");
			// FORM to input No of names
			function INPUT()
			{
			    document.write("<form name=\"TENDUOCNHAP\" method=\"\" action=\"\" >");
				var i;
			    for(i=0; i<N; i++)
					{
					     document.write("Name"+" "+i+":");
						 document.write("<input type=\"text\" name=\"input_name\" size=\"30px\" value=\" \" > <br/><br/>");
					}
				document.write("<input type=\"button\" name=\" \" onclick=\"SORT()\" value=\"Sortting!\" >");
				
			    document.write("</form>");
			}
            function SORT()
            {	
			    var i;
                for(i=0; i<N; i++)
					{
						arr[i] = document.TENDUOCNHAP.input_name.value;
						document.write(arr[i]);
					}			
				sortedARR=arr.sort();
				var x;
				for (x in sortedARR) 
				document.write(sortedARR[x]);
			}
				
					
	  </script>	   				
</body>
</html>

I don't know why that does not work? I can not sorting the array!
Please help me! Thanks!

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by thebluestar
0

Ok so first of all, your line var No = document.TEN.NoName.value; is called before you've typed anything into the box. If you want to use it properly, set it at the start of your INPUT() function. Also not sure why you're using N = eval(No); ? Are you looking for parseInt() maybe? (Also should be in the INPUT() function.)

But more importantly than those issues, your main issue is document.write. If you do that after the page is loaded(ie in your INPUT function), it will rewrite the whole page. So once you do that, you lose your headers, body, and even all your scripts/variables. So you can either rewrite the whole page in that function, or use something like document.body.innerHTML=...

function INPUT()
			{
				No = document.TEN.NoName.value;
				N = parseInt(No);
				var bText = "<form name=\"TENDUOCNHAP\" method=\"\" action=\"\" >";
				var i;
			    for(i=0; i<N; i++)
					{
					     bText += "Name"+" "+i+":";
						 bText += "<input type=\"text\" name=\"input_name\" size=\"30px\" value=\" \" > <br/><br/>";
					}
				bText += "<input type=\"button\" name=\" \" onclick=\"SORT()\" value=\"Sortting!\" >";
				
			    bText += "</form>";
				document.body.innerHTML = bText;
			}

That should get you started. You still need to fix your SORT() function.

0

Actually, I still do not understand the way the values which are inputted in the function Input to pass into sort function to Sort? Can you explain more for me? Thanks!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.