views:

42

answers:

2

I want to create a <textarea>. And I want to use JavaScript codes which is in the <textarea> as codes.

How can I do it?

+1  A: 

You can run the value of the textarea through the javascript eval() function, causing them to be evaluated as javascript.

Online example: http://jsbin.com/ohuqa/edit

Jonathan Sampson
+2  A: 

If you mean that you want to have the contents of the <textarea> parsed and evaluated as Javascript, you'd do something like this:

var script = document.getElementById('theIdOfTheTextarea').value;
eval(script);

You'd probably want to wrap that in a try/catch so that you could display an error:

try {
  eval(script);
}
catch (e) {
  alert("Error in the codes: " + e);
}
Pointy