
HTML Counter
In HTML, you can create a counter using JavaScript. Here’s an example of how you can implement a simple counter using HTML and JavaScript:
HTML:
<button onclick="increment()">Increment</button>
<button onclick="decrement()">Decrement</button>
<p>Counter: <span id="counter">0</span></p>
JavaScript:
var counter = 0;
function increment() {
counter++;
updateCounter();
}
function decrement() {
counter--;
updateCounter();
}
function updateCounter() {
var counterElement = document.getElementById("counter");
counterElement.textContent = counter;
}
In the above code, we have two buttons with onclick
attributes that call the increment()
and decrement()
functions when clicked. The counter variable keeps track of the current count, and the updateCounter()
function updates the counter display by modifying the text content of the <span>
element with the id “counter”.
When the “Increment” button is clicked, the counter is incremented by 1, and when the “Decrement” button is clicked, the counter is decremented by 1. The updated counter value is then displayed on the web page.
You can further customize the styling and layout of the counter using CSS to match your desired design.