I want to send an associative array through an event onclick
, when I send it, when I capture it, it appears, [object object]
so I try to send it string
using type JSON.stringify()
, I do it in the following way:
onclick event
$('#div').html('<div class="col-xs-6 text-center"><a href="#" class="on-primary edit-row" onclick=\' mostrarDatos("'+JSON.stringify(datos)+'"); \'><i class="fa fa-pencil" aria-hidden="true"></i></a></div>');
This is how it shows when I capture the error in the console
mostrarDatos("[{"id":"1","nombre":"Prueba","direccion":"calle 2 av Siemprevivas","telefono":"123332123","observaciones":"Asdasdasdasdas","correo":"[email protected]","fechaInicio":"2016-07-01","fechaFin":"2017-02-08","logo":"archivos/mpg.jpg","costo":"9","estado":"1","Municipio_id":"1259","Estado_id":"777","Empresa_id":"2","municipio":"Cali"}]");
The error is that you are putting double quotes inside a string delimited with double quotes, so that causes the string to break and give errors. You can see what's wrong even in the coloring that StackOverflow gives to the code:
An easy method would be to change the outside double quote to a single one. Then everything will work fine (and it colors well):
But that would require changing the original string a lot. It's easier to escape the quotes in the result of cJSON.stringify like this:
Here is a simplified example that works using that method: