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 :)

5
  • 1
    eval(controller + '.' + action + '(' + elementId + ')');, but that is not recommended Commented Sep 15, 2014 at 12:21
  • 2
    You're just building a String. You would need to eval it to see any action. However, from a security (and app design) point of view, that's generally a very bad idea. Commented Sep 15, 2014 at 12:21
  • eval( controller + '.' + action + '(' + elementId + ')' ) ; ??? Commented Sep 15, 2014 at 12:21
  • There is no need for eval! Commented Sep 15, 2014 at 12:22
  • @epascarello so what do you propose me to do ? thank you for answer Commented Sep 15, 2014 at 12:23

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.