Skip to content

Instantly share code, notes, and snippets.

@Arsfiqball
Last active July 26, 2021 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arsfiqball/8b5eb59111aee7aa9e01ffe7d3046b19 to your computer and use it in GitHub Desktop.
Save Arsfiqball/8b5eb59111aee7aa9e01ffe7d3046b19 to your computer and use it in GitHub Desktop.
Run javascript from a html textarea
<!DOCTYPE html>
<html>
<head>
<title>Run Script</title>
</head>
<body>
<textarea id="code">return 1 + 4</textarea>
<button onclick="run()">Run</button>
<pre id="result"></pre>
<script>
function run () {
const code = document.getElementById('code').value
const resultEl = document.getElementById('result')
const fn = new Function(code)
resultEl.innerHTML = fn()
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment