Код
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Счетчик посещений</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="counter.js" defer></script>
</head>
<body>
<h1>Добро пожаловать на сайт!</h1>
<p>Количество посещений: <span id="counter">0</span></p>
</body>
</html>
Код
let counter = 0;
const counterValue = document.getElementById('counter-value');
const incrementBtn = document.getElementById('increment-btn');
const decrementBtn = document.getElementById('decrement-btn');
const resetBtn = document.querySelector('#reset');
// To increment the value of counter
incrementBtn.addEventListener('click', () => {
counter++;
counterValue.innerHTML = counter;
});
// To decrement the value of counter
decrementBtn.addEventListener('click', () => {
counter--;
counterValue.innerHTML = counter;
});
// To reset the counter to zero
resetBtn.addEventListener('click', reset);
function reset() {
counter = 0;
counterValue.innerHTML = counter;
}