Вопрос

I'm searching for a couple of weeks to do this trick with jquery but everything failed so I'm posting it here.

Is it possible to store an effect name in a variable and then execute that effect on a div?
I mean something like this:

var effect = 'fadeOut';

$('#some_div').effect();

I hope you will understand what I'm searching for.

Это было полезно?

Решение

var effect = 'fadeOut';

var $div = $('#some_div');
$div[effect]();

Другие советы

Use brackets for the property access. Methods in JavaScript are properties too.

$('#some_div')[effect]();

Use bracket notation:

$('#some_div')[effect]();

This will run the function with the name equal to the value of effect.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top