problem when sending an array by onclick method, shows undefined

1

Good morning everyone, I have a problem sending an array by an "onclick" method when wanting to show the array sent I get "undefined"

onclick code:

firebase.database().ref("/4/").on("value", (dataSnapshot) => {
    dataSnapshot.forEach(function(item){
      var valor = item.val();
      var td = document.createElement('tr');
      var detalle = valor.detail;
      var arrayFinal = []
      for(var i in detalle){
          arrayFinal[i] = detalle[i];
      }
      console.log(arrayFinal);
      td.innerHTML = "<td>" + valor.nombre + "</td>" +" <td> <paper-button class='btn btn-primary' onclick=\"modal("+ arrayFinal +")\" raised> detalle  </paper-button></td>" + "<td>" + valor.valor + " <span class='text-danger'>  CL$ </span></td>";
      document.getElementById("ascap").appendChild(td);
  });
});

code of the method that the onclick calls "modal ()"

function modal(results){
   console.log(typeof(results));
   var newArray = JSON.stringify(results);
   console.log(results);
 };

impression of the variable arrayFinal:

  

[detail: "string", dateDoc: "date", datePag: "date", amountDecl:   "number", amountTotDoc: "number" ...]

console response of the modal function ():

  

my-home.html: 480 undefined my-home.html: 482 undefined

I tried to do the JSON.stringify before sending it, but at the time of printing it returns an empty array "{}"

Any ideas, what am I wrong?

    
asked by sebastian acuña 04.05.2017 в 15:31
source

1 answer

0

You can not paste an object as such in html code, in your case you have a JSON use the property JSON.stringify() to convert it to string and in this way you can paste it, Example:

firebase.database().ref("/4/").on("value", (dataSnapshot) => {
    dataSnapshot.forEach(function(item){
      var valor = item.val();
      var td = document.createElement('tr');
      var detalle = valor.detail;
      var arrayFinal = []
      for(var i in detalle){
          arrayFinal[i] = detalle[i];
      }
      console.log(arrayFinal);
      td.innerHTML = "<td>" + valor.nombre + "</td>" +" <td> <paper-button class='btn btn-primary' onclick='modal("+ JSON.stringify(arrayFinal) +")' raised> detalle  </paper-button></td>" + "<td>" + valor.valor + " <span class='text-danger'>  CL$ </span></td>";
      document.getElementById("ascap").appendChild(td);
  });
});

Here is a functional example of how you should do it:

function modal(results){
 console.log(typeof(results));
 console.log(results);
 };

arrayFinal = {detalle: "string", fechaDoc: "date", fechaPag: "date", montoDecl: "number", montoTotDoc: "number"}

console.log(arrayFinal);

document.getElementsByTagName("body")[0].innerHTML="<button onclick='modal("+ JSON.stringify(arrayFinal) +")'>funcion modal()</button>";

I hope this serves you, Regards;))

    
answered by 04.05.2017 / 16:38
source