fetch("https://especiales.larepublica.pe/congreso-votacion/prueba.json") .then(response => response.json()) .then(data => { const votaciones = data; let widthDevice = window.innerWidth; function renderEspeciales() { const especiales__container = document.querySelector('.especiales__container'); const mesesContainer = votaciones.map(votacion => { return ( `

${votacion.mes}

` ) }) especiales__container.innerHTML = mesesContainer.join(''); votaciones.forEach(votacion => { const especialPorMesContainer = document.querySelector(`#${votacion.id}`); const especialPorMes = votacion.especiales.map(especial => { return ( `
${especial.titulo}

${especial.titulo}

` ) }) const g = document.createElement('div') g.setAttribute("id", `especiales-${votacion.id}`) g.setAttribute("class", `votacionesContainer`) especialPorMesContainer.appendChild(g).innerHTML = especialPorMes.join('') const especiales = document.querySelectorAll(`.especial__${votacion.id}`); const onlyShowOne = (a) => { if (widthDevice < 500) { especiales.forEach(especial => { especial.style.display = 'none'; }) especiales[a].style.display = 'block'; } } onlyShowOne(0) const this__next__button = document.querySelector(`#next__button__${votacion.id}`) const this__prev__button = document.querySelector(`#prev__button__${votacion.id}`) const handleNext = () => { const especialesArray = Array.from(especiales); const a = especialesArray.findIndex(especial => especial.style.display === 'block') if (a < especialesArray.length - 1) { onlyShowOne(a + 1) } else { onlyShowOne(0) } } const handlePrev = () => { const especialesArray = Array.from(especiales); const a = especialesArray.findIndex(especial => especial.style.display === 'block') if (a > 0) { onlyShowOne(a - 1) } else { onlyShowOne(especialesArray.length - 1) } } this__next__button.addEventListener('click', handleNext) this__prev__button.addEventListener('click', handlePrev) }) } window.addEventListener("resize", function () { widthDevice = window.innerWidth; renderEspeciales() } ); renderEspeciales() })