Emmanual
Emmanual

Reputation: 124

Is there a way to parameterize a jquery function name and call it?

I'm trying to make a plugin for a modal and I want the users to be able to initialize the animate function picking from a jquery list of functions like fadeIn , SlideDown etc.

var myModal = new createModal({
    content: myContent,
   appendTo: ".modalSection"
});

$(".modal_trigger").on("click", function () {
   myModal.open();
});

createModal.prototype.open = function () {
    var functionVar = this.options.animateType;
    $(this.modal).fadeIn();
    //i want to replace this above line with the function name in this.options.animateType
}

Any way to do this ?

Upvotes: 1

Views: 55

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337733

As a jQuery object is, as the name implies, an object, you can access its properties as you would any standard object.

In this case you can use bracket notation to call a function held in one of those properties. For example:

createModal.prototype.open = function () {
  var functionVar = this.options.animateType;
  $(this.modal)[functionVar]();
}

Upvotes: 1

Related Questions