How do I run javascript code from a HTML input box?

Advertisements

I am currently trying to run JavaScript code from a HTML input box for my online code compiler project.

function runCode(){
     let code = document.getElementById('code').value
     console.log(code)
}

I was expecting it to actually run the code instead of just logging it as a string

>Solution :

Use eval()

run.onclick =() => {
  eval(editor.value)
}
<textArea id="editor">console.log('test')</textArea>
<button id="run">run</button>

Leave a ReplyCancel reply