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

178
Views
.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 answers
Answer question

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