1

I want to create a <textarea>, and I want to use JavaScript code which is entered into the <textarea>, as code.

How can I do it?

2 Answers 2

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.