JavaScript Eval a string to a callable JS function

I need to eval a function kept as string and then use it in JS code, here's the scenario:

Step 1

<textarea id="function_text">function test(e){ alert(e.id); }</textarea>

Step 2

var thatFunction = eval($("#function_text").val()); // returns undefined

Step 3

thatFunction.call({id: 100});

Is there a way to do that?

P.S. I'm aware of all security considerations, I just need 100 to be alerted at step 3!

Answer:1

I'm going to give you a warning, don't do it, but here is how:

var thatFunction = Function('return ' + $("#function_text").val())();
thatFunction.call({id: 100});
Answer:2

I am trying to create a simple modal using twitter bootstrap. The function of that modal is to take the name of the link clicked on, and use that name as a header. Each of the names are in a Django ...

I am trying to create a simple modal using twitter bootstrap. The function of that modal is to take the name of the link clicked on, and use that name as a header. Each of the names are in a Django ...

  1. passing data to bootstrap modal
  2. passing data to bootstrap modal angularjs
  3. passing data to bootstrap modal angular 2
  4. passing data to ngx-bootstrap modal
  5. passing data to modal bootstrap 4
  6. passing data to modal bootstrap 3

I started Android Studio in my newly updated OS X (Mavericks now). Now in Android Studio (also just updated btw) I get a message "error running git" Error:Abnormal build process termination: ...

I started Android Studio in my newly updated OS X (Mavericks now). Now in Android Studio (also just updated btw) I get a message "error running git" Error:Abnormal build process termination: ...

  1. error abnormal build process termination

I am using PHP to fill a textarea: <textarea text-input" id="Desc" rows="15" wrap="soft" name="Desc"><?php echo $Desc; ?></textarea> but the textarea is showing HTML tags in it, ...

I am using PHP to fill a textarea: <textarea text-input" id="Desc" rows="15" wrap="soft" name="Desc"><?php echo $Desc; ?></textarea> but the textarea is showing HTML tags in it, ...

  1. convert textarea html
  2. textarea convert html to text
  3. html convert input to textarea

i have a <div> like this. <div id="div" class="a b _c d _e f"></div> how can convert classes of <div> to a javaScript array? var cls = $('#div').attr('class'); cls = $(cls)....

i have a <div> like this. <div id="div" class="a b _c d _e f"></div> how can convert classes of <div> to a javaScript array? var cls = $('#div').attr('class'); cls = $(cls)....