Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery store effect name in variable

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.

like image 328
Jack Smith Avatar asked Jun 02 '26 04:06

Jack Smith


2 Answers

var effect = 'fadeOut';

var $div = $('#some_div');
$div[effect]();
like image 196
Peter Avatar answered Jun 03 '26 19:06

Peter


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

$('#some_div')[effect]();
like image 43
Ry- Avatar answered Jun 03 '26 18:06

Ry-