0

In a form I have a hidden field:

<input type="hidden" id="callback_function" value="myFunction" />

Now some JavaScript:

<script>
    $('.dataRow').click(function(){
        if($('#callback_function').val()){
            //same as executing myFunction();
            eval($('#callback_function').val()+'();');
        }
    });
</script>

This works but seems very inelegant. Is there a way to do this without using eval?

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Samuel Fullman
  • 1,262
  • 1
  • 15
  • 20

1 Answers1

3

It seems that the function is global (a property of window) so we don't need to eval it at all:

<script>
$('.dataRow').click(function(){
   var fnName = $('#callback_function').val();
   if(fnName && window[fnName]) {
       window[fnName]();
   }
});
</script>
Hamish
  • 22,860
  • 8
  • 53
  • 67