How can I put the value of a JSON
in an input? I have this code:
{"db":{"d_confirmado":49,"d_reservado":30,"d_no_consulta":7,"d_observado":0,"d_institucional":0,"d_club_salud":63,"d_madre_ni":6,"d_cia_seg":9}}
and I want to display it like this:
$('#db_confirmado').val(db_confirmado);
But what I want is for it to do a loop to see if the div
with the same name exists, then input the value.
NOTE: I do not want to write all the div
, but rather a walk through.
You're using jQuery, so you could use it
each()
to iterate through the different elements of the object. The format would be the following:Here is an example with the code you shared:
And an example that fills the HTML elements if the id exists and matches the index:
If I understood the problem correctly, the solution could be like this, where
miJson
is the JSON that you put in the question:Although here I leave you a sample of how you can do it without external Libraries, it is written in Native JS. You can modify the code to your needs.