0

I have a function that gets called from dynamically generated html, but its not being executed

The function.

$.check = function(s){
alert (s);
} 

....and then... the value of the contents of the following function object is the above function's call.

value is  var s = 'hey';$.check(s);

function( event, ui) {
//////The following does nothing, not even an error
      return ui.item.value;
       }

How do I execute the function stored in "value";

5
  • i am not sure if i got what you are asking but if you want to call the function in value then call like ui.item.value(); Commented Nov 26, 2014 at 10:41
  • Nah, this whole thing: ui.item.value, has "var s = 'hey';$.check(s);" stored in it. So i want to run that. Commented Nov 26, 2014 at 10:44
  • 1
    eval(ui.item.value); Commented Nov 26, 2014 at 10:48
  • Ah, @CerlinBoss you typed your comment as I typed my answer! Sorry, dude. Commented Nov 26, 2014 at 10:50
  • That makes sense. Let me test it. Commented Nov 26, 2014 at 10:50

2 Answers 2

1

The only way to do this in a browser is eval

eval(ui.item.value);

Node supports more advanced things like vm.runInNewContext(value,{/* context */}).

But beware of eval, it is dangerous. https://www.google.com/?q=eval+is+evil

Sign up to request clarification or add additional context in comments.

2 Comments

Well this guy here argues the basis of it being evil. nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood
Yeah, lots of arguments about it. I am mixed myself, which is why I say "dangerous". I prefer to avoid it whenever I can, but cannot always.
0

It's simple, just do:

function( event, ui) {
    if (typeof ui.item.value === 'function') {
        ui.item.value.call();
    }
}

Create that var s as a function, like so:

ui.item.value = function () {
    var s = 'hey';
    $.check(s);
}

2 Comments

I don't think this will seen as a function where u are checking if its a function, there's variable before the function itself.
But when you build that var s and the $.check, just wrap it in a function in your value

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.