[clug-progsig] Javascript how to write to a <textarea>

Doug Boyd douglasboyd at shaw.ca
Wed Feb 9 05:18:21 PST 2005


Hi guys,

I'm working on a Javascript program to solve the classic Hanoi Towers problem.
The recursive function and algorithm works fine, but I'm unable to write the 
'textstring' variable to the <textarea>.

Upon completion of the function, the windows resets.
I put the alert in to see what is happening.

Any suggestions?

<html>
<head>
<title>Recursion</title>
<script language="javascript">
  var textstring="";  //Unassigned var
  function solve(X, source, destination, hold){
    if(X>0){
       solve(X-1, source, hold, destination);
       textstring += source+" -> "+destination+"\n";
       document.myoutput.box.value=textstring;
       alert(textstring);
       solve(X-1, hold, destination, source);
    }
  }
</script>
</head>
<body bgcolor="lightblue">
<center>
<h2>Towers of Hanoi</h2>
<h3>By Doug Boyd</h3>
<br />
<form name="myoutput" 
onsubmit="solve(document.myoutput.totaldisks.value,1,3,2);">
  Enter total number of disks
  <input type="text" name="totaldisks" size="10">
  <input type="submit" value="Solve" />
  <br /><br /><br />
  <textarea name="box" id="box" cols="30" rows="10">Solution presented here.
  </textarea>
</form>
</center>
</body>
</html>

Thanks for taking a look,

Doug




More information about the clug-progsig mailing list