javascript - Trigger function from object using dynamic key -
i'm struggling on triggering function defined in object, pointing using dynamic key. code looks this:
$(function(){ var events = { test : function(){ console.log ('init'); } } $('#trigger').click(function(){ var e = $(this).data('event'); events[e]; }); });
so basically, there element #trigger attribute data-event="test" (or else), once gets clicked check events object if there function defined trigger , fire it. however, doesn't work way. can console log events object function isn't executed. doing wrong?
thanks!
invoke function ()
$('#trigger').click(function(){ var e = $(this).data('event'); events[e](); // ^^this });
Comments
Post a Comment