Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
How to use wp_localize_script() variable in js as a variable? (not as string)

I've added a field to our wordpress backend where I can write text into it. (for example "colorVariation + btnVariation) -> That should define a specifc order for the js variables later

I'm able to receive this text in my js file with the wp_localize_script function.

  wp_localize_script('wc-product-js', 'script_vars', array(
      'order' => get_field("wc_variation_order"),
      )
  );  

It seems to be, that this variable get's converted into a string when I try to use the variable in my js file. like that:

    var colorVariation = '_red';
    var btnVariation = '_male';        
    var order = script_vars.order;

    var varId = '.variation' + order;

My expected output would be ".variation_red_male" but the output is ".variationcolorVariation + btnVariation)

Is there any way to convert this string?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Alright. Finally I've found the function by myself. It's eval().

The eval() function is able to evaluate or executes an argument that is passed inside it. If the argument is an expression, this function will evaluate the expression and the argument is one or more JavaScript statements, eval() executes the statements.

Quote from https://www.codespeedy.com/convert-string-into-variable-name-in-javascript/

var colorVariation = '_red';
var btnVariation = '_male';        
var order = script_vars.order;

var varId = '.variation' + eval(order);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!