Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

314
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda