MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Interactive Button Click Counter0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
12.1Interactive Button Click Counter12.2Simple Form Validation12.3Image Slider (Basic)12.4Final Mini Project: To-Do List Application
HomeTutorialsJavaScript for BeginnersInteractive Button Click Counter
Simple Form Validation

Interactive Button Click Counter

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Welcome to your first real-world JavaScript project! In this lesson, we will construct a clean, responsive Interactive Click Counter Application featuring state persistence, increment/decrement controls, reset capabilities, and dynamic CSS styling based on state.


1. Application Specifications

Our Click Counter must provide:

  1. 1
    Interactive UI: Increment (+), Decrement (-), and Reset buttons.
  2. 2
    Dynamic Styling: Display green text for positive numbers, red text for negative numbers, and slate text for zero.
  3. 3
    Step Selection: Allow users to increment by 1, 5, or 10.
  4. 4
    State Persistence: Save the count in the browser's localStorage so refreshing the page preserves the count!

2. The HTML Markup (index.html)

1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>Counter App | MSK Institute</title>
7 <link rel="stylesheet" href="style.css">
8 <script src="counter.js" defer></script>
9</head>
10<body>
11 <div class="card">
12 <h2>Interactive Counter</h2>
13 <div id="counter-value" class="counter-display">0</div>
14 
15 <div class="controls">
16 <button id="btn-decrement" class="btn btn-danger">- Decrease</button>
17 <button id="btn-reset" class="btn btn-secondary">Reset</button>
18 <button id="btn-increment" class="btn btn-success">+ Increase</button>
19 </div>
20 
21 <div class="step-selector">
22 <label for="step">Step Value:</label>
23 <select id="step-select">
24 <option value="1" selected>1</option>
25 <option value="5">5</option>
26 <option value="10">10</option>
27 </select>
28 </div>
29 </div>
30</body>
31</html>

3. The CSS Stylesheet (style.css)

1* {
2 box-sizing: border-box;
3 font-family: system-ui, -apple-system, sans-serif;
4}
5 
6body {
7 background: #f1f5f9;
8 min-height: 100vh;
9 display: flex;
10 align-items: center;
11 justify-content: center;
12}
13 
14.card {
15 background: #ffffff;
16 padding: 2.5rem;
17 border-radius: 1rem;
18 box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
19 text-align: center;
20 width: 100%;
21 max-width: 400px;
22}
23 
24.counter-display {
25 font-size: 5rem;
26 font-weight: 800;
27 margin: 1.5rem 0;
28 transition: color 0.2s ease, transform 0.1s ease;
29}
30 
31.counter-positive { color: #10b981; }
32.counter-negative { color: #ef4444; }
33.counter-neutral { color: #64748b; }
34 
35.controls {
36 display: flex;
37 gap: 0.75rem;
38 justify-content: center;
39 margin-bottom: 1.5rem;
40}
41 
42.btn {
43 padding: 0.75rem 1.25rem;
44 font-weight: 600;
45 border: none;
46 border-radius: 0.5rem;
47 cursor: pointer;
48 transition: opacity 0.2s;
49}
50 
51.btn:hover { opacity: 0.9; }
52.btn-success { background: #10b981; color: white; }
53.btn-danger { background: #ef4444; color: white; }
54.btn-secondary { background: #cbd5e1; color: #1e293b; }
55 
56.step-selector {
57 font-size: 0.875rem;
58 color: #475569;
59}

4. The JavaScript Logic (counter.js)

1// 1. Select DOM Elements
2const counterDisplay = document.querySelector("#counter-value");
3const btnIncrement = document.querySelector("#btn-increment");
4const btnDecrement = document.querySelector("#btn-decrement");
5const btnReset = document.querySelector("#btn-reset");
6const stepSelect = document.querySelector("#step-select");
7 
8// 2. Initialize State from LocalStorage (or fallback to 0)
9let count = Number(localStorage.getItem("savedCount")) || 0;
10 
11// 3. Render Function to update UI & Colors
12function updateUI() {
13 counterDisplay.textContent = count;
14 
15 // Reset classes
16 counterDisplay.classList.remove("counter-positive", "counter-negative", "counter-neutral");
17 
18 // Dynamic conditional coloring:
19 if (count > 0) {
20 counterDisplay.classList.add("counter-positive");
21 } else if (count < 0) {
22 counterDisplay.classList.add("counter-negative");
23 } else {
24 counterDisplay.classList.add("counter-neutral");
25 }
26 
27 // Save to localStorage
28 localStorage.setItem("savedCount", count);
29}
30 
31// 4. Attach Event Listeners
32btnIncrement.addEventListener("click", () => {
33 const step = Number(stepSelect.value);
34 count += step;
35 updateUI();
36});
37 
38btnDecrement.addEventListener("click", () => {
39 const step = Number(stepSelect.value);
40 count -= step;
41 updateUI();
42});
43 
44btnReset.addEventListener("click", () => {
45 count = 0;
46 updateUI();
47});
48 
49// Initial render on page load
50updateUI();

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What method is used to persist data in the browser so that it survives page refreshes?
Question 2
Why is Number(localStorage.getItem("savedCount")) necessary?
Question 3
Why is creating a dedicated updateUI() function considered clean architecture?
Question 4
How do you read the currently selected numeric value of an HTML <select> element?
Question 5
What CSS transition property was added to .counter-display to ensure smooth color shifting?
Next Lesson

Simple Form Validation

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Clean Code PracticesSimple Form Validation

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext