skip to Main Content

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.

2

Answers


  1. you want this slove?

    $('.class')[t]();
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search