JSFiddle - React, Tailwind, and code Playground
by Sergei Sokolov
HTML
<p>Месяц: Хабр</p>
<button id="month-prev">←</button>
<button id="month-next">→</button>
JavaScript
/**
* для вопроса https://qna.habr.com/q/1024604
* Как изменить переменную при нажатии кнопки js?
*/
var month = new Date().getMonth();
const p = document.querySelector('p');
const update = (delta = 0) => {
const D = new Date();
D.setMonth(month + delta);
month = D.getMonth();
p.textContent = `Месяц: ${(1 + month).toString().padStart(2, '0')}`;
}
update();
document.getElementById('month-prev')
.addEventListener('click', () => update(-1));
document.getElementById('month-next')
.addEventListener('click', () => update(1));