Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - use variable as function name

Tags:

jquery

In Jquery i'd like to disable any plugin I want by changing a variable name. However the following code doesnt work

function disablePlugin(functionName) {
    $('#divID').functionName('disable')
}

disablePlugin('sortable');

any ideas about how I manage to do this?

like image 322
Matt Avatar asked Mar 07 '12 15:03

Matt


2 Answers

This is how you would do that:

function disablePlugin(functionName) {
  $('#divID')[functionName]('disable')
}

disablePlugin('sortable');

This works because someObject.foo is the same thing as someObject['foo']

like image 120
nicholaides Avatar answered Oct 04 '22 20:10

nicholaides


To invoke the function passed in as a string, you could do

function disablePlugin(functionName) {
    $('#divID')[functionName]('disable')
}

disablePlugin('sortable');
like image 41
Niclas Sahlin Avatar answered Oct 04 '22 21:10

Niclas Sahlin