JSFiddle - React, Tailwind, and code Playground

by agrilo

HTML

<button onclick="previous()">
    PREV
</button>
<pre id="output"></pre>
<button onclick="next()">
    NEXT
</button>

JavaScript

var today = new Date();
var availableMonthsToNavigate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

// Init
var selectedMonth = availableMonthsToNavigate
  .find((month) => month === today.getMonth() + 1);
/* document.getElementById('output').innerHTML = selectedMonth; */

function previous() {
  selectedMonth = availableMonthsToNavigate
    .find((month) => month === selectedMonth - 1) ? availableMonthsToNavigate[selectedMonth - 1] :
    availableMonthsToNavigate[selectedMonth];
  document.getElementById('output').innerHTML = selectedMonth;
  console.log(selectedMonth);
}

function next() {
  selectedMonth = availableMonthsToNavigate
    .find((month) => month === selectedMonth + 1) ?
    availableMonthsToNavigate[selectedMonth + 1] :
    availableMonthsToNavigate[selectedMonth];
  document.getElementById('output').innerHTML = selectedMonth;
   console.log(selectedMonth);
}