I have the following div:
<div id="foo" data-callback="function(){console.log(1)}"></div>
I want to be able to execute the div’s callback string as a JavaScipt function like this:
($('#foo').data('callback'))()
But this obviously won’t work. Is there any way to do this?
Here’s a JSFiddle.
I suggest not storing code as a
data-attribute. Make a function, then just store the function name as the attribute.Then you can do
window[$('#foo').data('callback')]().EDIT: If you don’t want to do this, you can also make use of the
onclickevent (or any (built-in) event for that matter).Then you can use
.propto get the event (as a function), and then use it.