60 lines
1.8 KiB
HTML
60 lines
1.8 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Counter Example</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
|
</head>
|
|
<body>
|
|
<div class="container py-5">
|
|
<h1>Counter</h1>
|
|
|
|
<div class="row mt-4">
|
|
<div class="col col-12 col-sm-10 col-lg-6">
|
|
<div class="input-group mb-3">
|
|
<span class="input-group-text" id="basic-addon1">Count:</span>
|
|
<input type="text" id="count" class="form-control" value="0" readonly />
|
|
</div>
|
|
<button id="incrementButton" class="btn btn-primary w-100">Increment</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
'use strict';
|
|
|
|
(() => {
|
|
// ===== DOM & VARS =====
|
|
const countEl = document.querySelector('#count');
|
|
const btnEl = document.querySelector('#incrementButton');
|
|
|
|
let counter = { count: 0 };
|
|
|
|
// ===== INIT =====
|
|
const init = () => {
|
|
btnEl.addEventListener('click', onClickIncrement);
|
|
};
|
|
|
|
// ===== EVENT HANDLER =====
|
|
function onClickIncrement() {
|
|
// variante 1
|
|
// let count = counter.count;
|
|
// count++;
|
|
// counter = { count: count };
|
|
|
|
// Variante 2
|
|
// counter = { count: ++counter.count }; // hier ist der präfix-inkrement wichtig - der post-inkrement würde ins "leere" gehen, da er erst zugewiesen wird und dann erhöht und diese somit verloren ginge
|
|
|
|
// Variante 3 - direkt counter.count erhöhren
|
|
counter.count++;
|
|
countEl.value = counter.count;
|
|
}
|
|
|
|
// ===== CALL INIT =====
|
|
init();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|