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 :: react focus textarea 
Javascript :: avascript regex email 
Javascript :: how to install vue 
Javascript :: how to import js via script in react 
Javascript :: javascript check if url returns 200 
Javascript :: timepicker in jquery 
Javascript :: accessing object properties with bracket notation 
Javascript :: $q.platform.is.mobile 
Javascript :: js join 
Javascript :: object values 
Javascript :: plotly in react 
Javascript :: alpinejs with select 
Javascript :: javascript extract date from string 
Javascript :: preload 
Javascript :: how to serve html node server 
Javascript :: javascript set class on div 
Javascript :: Passing components as children in react 
Javascript :: how to take screenshot of desktop using electron js 
Javascript :: double click in js 
Javascript :: get server by id discord.js 
Javascript :: merge objects js 
Javascript :: mongodb data types 
Javascript :: reset select option jquery 
Javascript :: jquery find 
Javascript :: ejs display variable 
Javascript :: contains duplicate leetcode solution javascript 
Javascript :: How to do a timer angular 
Javascript :: equivalent method load jquery with javascript 
Javascript :: electron new window 
Javascript :: javascript compare two arrays of objects 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =