javascript - calling the function from <div> attribute? -
for example, have next code:
var sale = {}; sale.maincontent = function(p) { alert("this handler main-content"); }
and have next html div:
<div id="content-data" data-handler="maincontent">{"name":"john"}</div>
i try do next things - parsing content of div in jquery, this:
var cd=$("#content-data"); var obj = jquery.parsejson(cd.text());
and when attribute fo data-handler:
var hname=cd.attr("data-handler");
so, next step - call function name hname = 'maincontent' in sale object this(?):
sale.hname(obj);
how can call function name in variable?
you can -
sale[hname](obj);
Comments
Post a Comment