Based on a solution by Cerbrus here;
Passing function with custom data attribute
Is it possible to pass parameters as part of the custom attribute? So something like;
<div data-myattr="hello(foo)"></div>
Based on a solution by Cerbrus here;
Passing function with custom data attribute
Is it possible to pass parameters as part of the custom attribute? So something like;
<div data-myattr="hello(foo)"></div>
Why?
I mean, yes, if you want to do a RegEx match for contents inside the parens, separated by commas, and then inject them. But that's madness.
Why not just make a second data-attribute?
<div data-command="myFunction" data-params="one,two,three"></div>
And then grab everything like:
var command = document.querySelector("[data-command]"),
vars = command.dataset.params.split(","),
funcName = command.dataset.command;
window[funcName](vars[0], vars[1], vars[2]);
Or alternatively:
window[funcName].apply(undefined, vars);