How to access a JSON value and enter data-id?

Asked

Viewed 60 times

1

I have this JSON:

{draw: 1, recordsTotal: 16, recordsFiltered: 16, data: [,…]}

and within the data, for example a record:

0: ["1", "FooBar", "00.000.000/0000-00", "13600-000", "Rua Santos Dumont", "Curitiba", "Jardim X", "ID_1"]

How can I catch only the number of ID_1 and put in this data-id:

<a data-toggle="modal" data-target="#infoModal" data-id:"**COLOCAR AQUI**" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a>

1 answer

2


You just need to iterate obj.data and generate a <a> with the array’s input.

Example:

var obj = {
  draw: 1,
  recordsTotal: 16,
  recordsFiltered: 16,
  data: [
    ["1", "FooBar", "00.000.000/0000-00", "13600-000", "Rua Santos Dumont", "Curitiba", "Jardim X", "ID_1"]
  ]
}

obj.data.forEach(function(data) {
  var a = '<a data-toggle="modal" data-target="#infoModal" data-id= "' + data[7] + '" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130">' + data[4] + '</i></a>';
  document.body.innerHTML += a;
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.