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

Shawn sgrover at open2space.com
Wed Feb 9 06:32:29 PST 2005


At first glance, it looks right.  The reference should be along the lines of 
document.formName.textareaName.value.  Maybe change your routine a little so 
that you do an alert on document.myoutput.box.value to see what you get?

Another point, each time your function is being called, you are replacing the 
contents of the text box.  This may lead to problems - you are manipulating a 
global variable on each call of the function (or each iteration) - this leads 
to unpredictable results.  In a case like this I would probably pass the 
modified string into the function, and have the function treat the string as 
a local variable/parameter, then return the string when the end condition is 
met.

Let us know if you get this resolved.  If not, I'll take a better look at it 
later tonight (gotta run right now...)

HTH

Shawn

On Wednesday 09 February 2005 06:18, Doug Boyd wrote:
> 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
>
>
> _______________________________________________
> clug-progsig mailing list
> clug-progsig at clug.ca
> http://clug.ca/mailman/listinfo/clug-progsig_clug.ca



More information about the clug-progsig mailing list