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

179
Vistas
.data() returns string when table needed

In my html I've got a table: <table id="table" data-keys="{{keys}}"> where keys are an array (I later succesfully call {% for k in keys %}).

But when in my js I call: $("#table").data("keys") the result is string: "['', 'Value1', 'Value2', 'Value3']" Is there anything smart that I can do to actually get the table or do I simply need to parse the string and convert it by myself?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to use double quotes for the string to be valid JSON. It will then be automatically parsed by jQuery.

const a = $('#a').data("keys");
const b = $('#b').data("keys");

console.log(typeof a, a)
console.log(typeof b, b)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="a" data-keys='["", "a", "b", "c"]'></div>
<div id="b" data-keys="['', 'a', 'b', 'c']"></div>

https://api.jquery.com/data/

Every attempt is made to convert the attribute's string value to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) [...] When a string starts with '{' or '[', then jQuery.parseJSON is used to parse it; it must follow valid JSON syntax including quoted property names. A string not parseable as a JavaScript value is not converted.

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