Search code examples
javascripttextareaevalexecute

use eval to evaluate a javascript snippet in a textarea?


I'm at my first hackathon and trying to finish my project. I am very very new the javascript... everything I know I literally learned in the last 2 hours. That being said...

So I know that eval is not the greatest thing to use, but I'm trying to write a simple program in which you can input a javascript snippet into a textarea, click an execute button, and have the javascript execute inside another textarea. I'm trying to stay away from jquery for now, because I want to get the really basic idea down before I add another level of complexity, which is why I'm not using id's.... but if jquery is the only way to do this, then I guess I'll have to pony up and learn it in the next 8 hours.

Code as follows (ish):

function executeJS ()
{
    var result = eval(game.input.value);
    game.execute.value=result;
}
<head>
    <body>
    <H1>PRogram</H1>
            <form name="game"> 

            <textarea name="execute" rows="5" cols="30" value=""></textarea><br>
            <textarea type="text" name="input" rows="10" cols="30" value=""></textarea>

            <input type = "button" value = "guess" onclick = "executeJS()</input>
            </form>
    </body>
</head>

I'm not getting an output in my execute box.

Any insight would be much appreciated.


Solution

  • "game" isn't a variable. it's a DOM element name.
    if you want to get it's object, give it an id let's say "game", and use document.getElementById('game')

    • Note that your <head> surround the <body>
    • Your javascript code isn't inside <script></script tag.