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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With