0

How can I pass a part of an expression as an argument as a string? Something like this:

let t = "val";
$('.class').t();

UPD

enter image description here

enter image description here

UPD 2

My mistake, I was just redefining an already declared variable.

VLAZ
  • 26,331
  • 9
  • 49
  • 67

2 Answers2

1

you want this slove?

$('.class')[t]();
小小落
  • 109
  • 1
  • 2
1

You can use Bracket notation to access the property of an object dynamically:

let t = "val";
$('.class')[t]();

This will call the method with the name that matches the value of the t variable on the jQuery object with the class. If the method exists on the object, it will be called with the appropriate arguments.

Mamun
  • 66,969
  • 9
  • 47
  • 59