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

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

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 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