Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda