Monday, 15 March 2010

Calling and Passing Javascript parameters as part of a HTML5 data attribute -



Calling and Passing Javascript parameters as part of a HTML5 data attribute -

based on solution cerbrus here;

passing function custom info attribute

is possible pass parameters part of custom attribute? like;

<div data-myattr="hello(foo)"></div>

why?

i mean, yes, if want regex match contents within parens, separated commas, , inject them. that's madness.

why not create sec data-attribute?

<div data-command="myfunction" data-params="one,two,three"></div>

and grab 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);

javascript html5 custom-data-attribute

No comments:

Post a Comment