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

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

0

Use JSON.parse()

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

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