Pass javascript function as data-* attribute and execute

One way is to use eval()

jQuery(".container").on("click", "button.marker", function (e) {
    var callback = jQuery(e.currentTarget).data("callback");

    var x = eval(callback)
    if (typeof x == 'function') {
        x()
    }
});

Demo: Fiddle

Note: Make sure it is safe in your environment, ie there is no possibility of script injection because of bad input from users

  • Why is using the JavaScript eval function a bad idea?
  • When is JavaScript’s eval() not evil?
  • eval() isn’t evil, just misunderstood
  • Eval is Evil, Part One

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)