I'm having a JS problem and it could be that I'm just stuck, but maybe one of you could help me out.
I have the following index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Calendar</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="stylesheets/base.css">
<link rel="stylesheet" href="stylesheets/content.css">
<link rel="stylesheet" href="stylesheets/calendar.css">
<script
type="application/javascript"
src="scripts/try.js">
</script>
<script src="scripts/jquery-3.3.1.min.js"></script>
</head>
<body
onload="init()">
<div id="content">
<div class="year">
<ul>
<li id="year">year</li>
<li class="prev" id="prevmonth" onclick="changeYear()">❮</li>
<li class="next" id="nextmonth" onclick="changeYear2()">❯</li>
</ul>
</div>
<hr>
<div class="month">
<ul>
<li id="month">month</li>
<li class="prev" id="prevyear" onclick="changeMonth()">❮</li>
<li class="next" id="prevyear" onclick="changeMonth2()">❯</li>
</ul>
</div>
</div>
<div id="calendar">
<table id="tablecalendar">
</table>
</div>
</body>
</html>
which loads init(). This function contains the following code:
function init() {
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
let monthsextension = ["nothing"];
var months2 = monthsextension.concat(months);
today = new Date();
var month2 = today.getMonth();
var year = today.getFullYear();
document.getElementById("month").innerHTML = months[month2];
document.getElementById("year").innerHTML = year;
displayC();
}
the function dispayC, has two functions to count up or down from the year:
function changeYear(){
var year = document.getElementById("year").innerHTML;
var year = year-1;
document.getElementById("year").innerHTML = year;
}
function changeYear2(){
var year2 = document.getElementById("year").innerHTML;
console.log(year2);
console.log(year2+1);
year2 = year2+1;
console.log(year2);
document.getElementById("year").innerHTML = year2;
}
now, function "changeYear" works as I would like it, but "changeYear2" doesn't add 1 to the year, but adds the digit one to the year.
Example:
"2019" becomes "20191"
with a second click on the counter arrow it becomes "201911"
How come I can subtract but not add to the year?
Hope it's not too much code, I try to cut it down as much as I could.