Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a button that increments a counter when clicked?

I am trying to make a button that increments a counter's value by 1 when clicked. My code, however, doesn't seem to work.

var count = 1;
var button = document.querySelector("#increment");

button.addEventListener("click", function() {
  var increment = document.getElementById("#count");
  increment.value = count;
  count++;
});
<h4>Current count: <span id="count">0</span></h4>
<div class="container">
  <button id="decrement">Decrement</button>
  <button id="increment">Increment</button>
</div>
like image 274
Min Kyung Kwon Avatar asked Jun 04 '20 07:06

Min Kyung Kwon


People also ask

How do I make an increment button in HTML?

First, we need to HTML div tag for the following elements: Increment Button: HTML container for increment button with id “increment-count. Total count: HTML container where total count text is displayed. Decrement Button: HTML container for decrement button with id “decrement-count.

How do you increment a counter in Python?

In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1. After writing the above code (python increment operators), Ones you will print “x” then the output will appear as a “ 21 ”. Here, the value of “x” is incremented by “1”.

How do you increment a variable in HTML?

length; let num; for (let num = 0; num < list; num++) { $('. number'). append(num); // The output is 0123 for all the numbers... }

What is counter-increment?

The counter-increment property increases or decreases the value of one or more CSS counters. The counter-increment property is usually used together with the counter-reset property and the content property. Default value: none.


Video Answer


2 Answers

You don't need # in getElementById and use innerHTML to set value. Don't use querySelector when you can get by id.

Like this:

let count = 0;
const button = document.getElementById("increment");
const button2 = document.getElementById("decrement");
const textHolder = document.getElementById("count");
textHolder.innerHTML = count;

button.addEventListener("click", function() {
  textHolder.innerHTML = ++count;
});

button2.addEventListener("click", function() {
  textHolder.innerHTML = --count;
});
<h4>Current count: <span id="count">0</span></h4>
<div class="container">
  <button id="decrement">Decrement</button>
  <button id="increment">Increment</button>
</div>
like image 128
V. Sambor Avatar answered Sep 29 '22 07:09

V. Sambor


Your code have some issues

  1. Use # in query selector, remove it, it use in jquery
  2. Wrong attribule value change to innerText
  3. Change querySelector to getElementById to get id

var count = 1;
var button = document.getElementById("increment");

button.addEventListener("click", function() {
  var increment = document.getElementById("count");
  increment.innerText = count;
  count++;
});
<h4>Current count: <span id="count">0</span></h4>
<div class="container">
  <button id="decrement">Decrement</button>
  <button id="increment">Increment</button>
</div>
like image 23
Hien Nguyen Avatar answered Sep 29 '22 07:09

Hien Nguyen