0

I wanna call a function from an object in js

var elementId = event.target.id;
var controller = $('#' + elementId).data('controller') + 'Controller';
var action = $('#' + elementId).data('action');

this is my data

right call would look like this

FilterController.getCurrentFilter('hosts-button');

but how do i build it with my variables elementId / controller / action ?

my try

controller + '.' + action + '(' + elementId + ')'; 

don't work

thank you very much for help :)

Ilario Engler
  • 2,419
  • 2
  • 27
  • 40

1 Answers1

2

Bracket notation will work ass long as it is in window/global scope.

window[controller][action](elementId);

If it is not in window/gloabl scope, you will need an object that holds the first level. [In reality that is a better solution]

epascarello
  • 204,599
  • 20
  • 195
  • 236