jQuery call function from a string
You can use the bracket notation to access the member with a string containing the identifier:
var target = 'next';
$("foobar")[target](); // identical to $("foobar").next()
If you're wanting to use jQuery, the answer is quite elegant. Because jQuery is an object (which can be accessed like an array) - you can use $("selector")[target]()
.
Examples:
const target = 'next';
jQuery("selector")[target]();
This will work if you know that you can trust the input. However, if you're not sure of this, you should check that the function exists before trying to run it otherwise you'll get an error.
const target = 'doesNotExist';
const fn = jQuery('selector')[target];
if (jQuery.isFunction(fn)) {
fn[target]();
}