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

128
Visualizações
Use text from DOM element as JSON object with JavaScript

I have a div in my webpage that contains some valid JSON string. I get its value using the text() function in jQuery like this:

$("div.selector").text();

The JSON string contains some new lines and tabs. It is perfectly valid and matches the formatting expected by Google Tag Manager Enhanced eCommerce (https://developers.google.com/tag-manager/enhanced-ecommerce).

The problem I am facing is that using the following prevents the event from being registered

dataLayer.push($("div.selector").text());

while pasting the JSON inside push() manually registers the event properly:

dataLayer.push(VALID JSON);

Using

dataLayer.push($("div.selector").text());

pushes a text string in dataLayer that looks like this

{\n\t\t\"event\": \"someEvent\",\n\t\t\"eventCategory\": \"Order Update\"

How can I push valid JSON that has been extracted using text()?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use JSON.parse()

eg: dataLayer.push(JSON.parse($("div.selector").text()));

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