I am developing a portfolio and in the following code you will see that I collect the information from a file .json , until here there is no problem. For those who do not use fetch , I leave the information here.
Code:
targetAbout.addEventListener('click', () => {
document.querySelector('#row').style.display = 'block';
console.log('Mostrado: ' + targetAbout + hora());
});
// Mostramos contenido por medio de ajax y json.
var respuestaHTML = document.querySelector('#elem');
var tpl = '';
fetch('json/productos.json', {mode: 'no-cors'})
.then((response) => {
return response.json()
})
.then((category) => {
category.forEach((elem) => {
tpl += '<img src="' + elem.thumbnailUrl + '"/>'
+ '<article class="elem-title"><p>' + elem.title + '</p></article>';
});
respuestaHTML.innerHTML = tpl;
}).catch((err) => {
console.log('Ha fallado: ' + err);
});
I retrieve the information without problem and shows it in <div>
but my question is:
If what I want is that every time I collect the data from a document .json I show it in a div
with the same id of the first one? That is, create an element every time you have to show information.
The ID would be elem :
<section id="row">
<article id="elem">
</article>
</section>
Should I use createElementById? How do I make it in a div
with a certain id?
I would appreciate it in Javascript, not using any frameworks.