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

125
Views
Use texto del elemento DOM como objeto JSON con JavaScript

Tengo un div en mi página web que contiene una cadena JSON válida. Obtengo su valor usando la función text() en jQuery así:

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

La cadena JSON contiene algunas líneas y pestañas nuevas. Es perfectamente válido y coincide con el formato esperado por Google Tag Manager Enhanced eCommerce ( https://developers.google.com/tag-manager/enhanced-ecommerce ).

El problema al que me enfrento es que usar lo siguiente evita que se registre el evento

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

mientras pega el JSON dentro de push() registra manualmente el evento correctamente:

 dataLayer.push(VALID JSON);

Usando

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

empuja una cadena de texto en dataLayer que se ve así

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

¿Cómo puedo enviar un JSON válido que se ha extraído usando text() ?

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

0

Usar JSON.parse()

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

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!