I want to remove the previous result of a query to a Spotify API and keep the new query, the code that prints the query in the DOM is this:
export function mostrarInfo(albumes){
albumes.forEach(info => {
if(info.album_type === 'album'){
const infoArtista = {
nombre: info.name,
fecha: info.release_date,
tracks: info.total_tracks,
foto: info.images[0]
}
const div = document.createElement('div');
div.classList.add('col-4');
div.classList.add('mb-5');
div.innerHTML = `
<div class="card_group">
<div class="card">
<img src="${infoArtista.foto.url}" class="card-img-top img-fluid">
<h2 class="titulo ml-2">${infoArtista.nombre}</h2>
<h4 class="ml-2">Tracks: ${infoArtista.tracks}</h4>
<p class="ml-2">Fecha de lanzamiento: <span class="badge badge-pill badge-warning">${infoArtista.fecha}</span>
</p>
</div>
</div>
`;
artistaContenido.appendChild(div);
}
})
}
And the code where I want to remove the previous result is this:
const resultado = document.querySelector('#artista_preferido_resultado div');
if(resultado !== null){
resultado.remove();
}
This only removes the first div from the first query and prints the result of the second.
Try to delete all the content of the div where you show the albums every time you go to show a new result: