Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute effect specified by string

Tags:

jquery

effects

I am currently trying to execute effects with jQuery. However, I do not want want to do anything like $("#something").hide(), I would like to specify the effect in a string. Here is an example:

var selector = "#myElement";
var effectString = "hide";
$(selector).effect(effectString);

I looked this code up, and apparently it did work that way, but for some reason the effect() method seems to have been removed.

My question is if there is any alternative that I could use. Or has a feature like this been intentionally removed? I am using jquery-3.2.1.

Thanks in advance for any helping thoughts.

like image 944
Nickname Avatar asked Mar 07 '26 03:03

Nickname


1 Answers

As the response to a selector is an object, you can use bracket notation to access the method names using a variable and invoke them, like this:

var selector = "#myElement";
var effectString = "hide";
$(selector)[effectString]();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Part of this <span id="myElement">string</span> will be hidden
</div>
like image 133
Rory McCrossan Avatar answered Mar 08 '26 18:03

Rory McCrossan