Problem when passing a Json via onclick to javscript

0

I am trying to send an array using onclick in a function, for that I use json. But it marks me a syntax error when I send it.

By the way, once I get the json, how can I go through his data? Greetings.

Error by console.

Code:

<td>
                          <select id="OBJ<?php echo $idCriterio;?>" name="COE" onchange="FunSumT(<?php echo json_encode($criterio); ?> )">
                            <option value='0'>0</option>
                            <option value='1'>1</option>
                            <option value='2'>2</option>
                            <option value='3'>3</option>
                            <option value='4'>4</option>
                            <option value='5'>5</option>
                          </select>
                        </td>

Code of my function js

function FunSumT(criterio){
                      var array=JSON.stringify(criterio);
                      console.log(array);
       }
    
asked by Luis Fernando Zumaran 02.06.2018 в 23:09
source

1 answer

0

How about? As far as I can see in the image that you share (since the entire line is not in development). That the problem that you have is the quotes. Try the following:

 onchange='FunSumT(<?php echo json_encode($criterio); ?> )'

Because leaving double quotes and the json also uses them breaks everything.

Try it and tell me if it was the solution.

A piece of advice that I can give you since I saw on the labels that you put on that you use jquery. That before putting the function in the html do everything from a javascript. To make it more orderly and easier for you to identify mistakes. For example, do something like this:

$('select[name="COE"]').on('change', function() {
    console.log("Aca escribis el codigo que necesites");
});

If you need that in have data then you add them in the following way:

<select data-id="<?=$idCriterio;?>" data-ejemplo="test">

An example of how to obtain such data can be:

var id=$('select').data("id");

In the case that this html is added after the moment that the JS charged. You can use the function bind () and unbind (). I leave the documentation of them below to not make this very extensive. Jquery .bind ()

Greetings!

    
answered by 03.06.2018 / 00:48
source