Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

counter in javascript

## MY HTML
<button id='counter'>Click Me To Count</button>
<span id='result'>1</span>

## MY JAVASCRIPT
let counter = document.getElementById("counter")
let result = document.getElementById("result")
let count = 1

counter.addEventListener("click", () => {
  count += 1
  result.innerHTML = count
})
Comment

counter in html and js

<!-- HTML ->
<button id="btn-count">Count</button>
<p id="result"></p>

// JavaScript
const btnCount = document.getElementById('btn-count');

btnCount.addEventListener('click', () => {
  	let counter = 0;
  	counter++;
  	document.getElementById('result').innerText = counter;
});
Comment

counter javascript

//simple counter in javascript 

// this is button element variable
const button = document.querySelectorAll(".btn")

//this variable for display counter number
const h3 = document.querySelector(".counter")
//Count variable
let count = 0;

//foreach method
button.forEach((btn) => {
  //event listener function       
  btn.addEventListener("click", (e) => {
    //event targeting element  
    let target = e.currentTarget

    //condition for counter variable
    if (target.innerText == "reset") {
      count = 0;
    }
    else if (target.innerText == "next") {
      count++;
    }
    else if (target.innerText == "prev") {
      count--;
    }
    //text contains according to the if condition
    h3.innerHTML = count;

  })

})









Comment

PREVIOUS NEXT
Code Example
Javascript :: how to see my timezone using js 
Javascript :: alert javascript 
Javascript :: mongodb add 1 to field 
Javascript :: angular.toJson 
Javascript :: set property js 
Javascript :: react select error handle 
Javascript :: js filter 
Javascript :: trim a string in javascript 
Javascript :: Slice and Splice -Javascript 
Javascript :: how to count seconds in javascript 
Javascript :: python minify json 
Javascript :: call a function of another component vue 
Javascript :: json to csv javascript 
Javascript :: how to pick date from datepicker in selenium 
Javascript :: download xlsx file javascript 
Javascript :: how to copy array of objects in javascript 
Javascript :: nds npm 
Javascript :: object assign 
Javascript :: callback in react 
Javascript :: javascript syntax 
Javascript :: how to use cookies in react class component 
Javascript :: jquery get all data attributes values 
Javascript :: encrypt js 
Javascript :: add word in string in javascript 
Javascript :: navigate json object javascript 
Javascript :: unzip array javascript 
Javascript :: javascript date to html date input 
Javascript :: javascript add query string to url 
Javascript :: javascript integer to binary 
Javascript :: custom processing datatables 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =