Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
           CodeHim.com

Free Web Design Code & Scripts

       
  • Home
  • Code Snippets
    • Accordion
    • Creative
    • Carousel
    • Date & Time
    • Gallery
    • LightBox
    • Menu & Nav
    • Modal
    • Others
    • Portfolio
    • Social Media
    • Text & Input
    • Video Player
    • Zoom
  • HTML & CSS
  • Bootstrap
  • JavaScript
  • Collections
  • About
  • Contact
  • Buy Me a Coffee

Home / Vanilla JavaScript / Spacebar Hit Counter JavaScript Code

Spacebar Hit Counter JavaScript Code

January 22, 2024January 19, 2024 by Asif Mughal
Spacebar Hit Counter JavaScript Code
Share This:
Code Snippet:Spacebar counter
Author: Murilo Polese
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 4,892
License: MIT
Edit Code online: View on CodePen
Read More
 Demo
Download (File path is not allowed!)

This lightweight JavaScript code snippet helps you to create spacebar hit counter. It is based on a simple idea to count the spacebar hits. Simply, it executes the keyup function, match the spacebar keycode, and increase the hit value using increment operator.

Whether you’re working on a game or web app in which you need to count spacebar hits, this simple code snippet equally helpful.

How to Create Spacebar Hit Counter in JavaScript

1. First of all, create a span element with a class name “hits” that will holds the number of hits counted by JS function. Similarly, create a button or anchor link with onclick attribute to register a click event for resetHits() function. Also, define a class name “tryagain” for the button, it will be used to reset the counting variable. The complete HTML structure for the counter is as follows:

<div id="activity">
  <h1 id="counter">You have hit the spacebar <span class="hits">0</span> times.</h1>
  <a href="#" onclick="resetHits()" class="tryagain">RESTART</a>
		</div>

2. Add the basic CSS style for the hits counter. Basically, these styles are optional, you can go without them.

body {
			margin: 0;
			font-family: 'Open Sans', sans-serif;
			position: absolute;
			width: 100vw;
			height: 100vh;
			overflow: hidden;
			display: table;
		}

		#activity {
			display: table-cell;
			text-align: center;
			vertical-align: middle;
		}


		#result {
			text-transform: uppercase;
		}

		.tryagain {
		background-attachment: scroll;
		background-clip: border-box;
		background-color: rgb(127, 206, 119);
		background-image: none;
		background-origin: padding-box;
		background-size: auto;
		border-bottom-left-radius: 16px;
		border-bottom-right-radius: 16px;
		border-top-left-radius: 16px;
		border-top-right-radius: 16px;
		box-sizing: border-box;
		color: rgb(255, 255, 255);
		cursor: pointer;
		display: inline-block;
		font-family: 'Open Sans', sans-serif;
		font-size: 32px;
		font-stretch: normal;
		font-style: normal;
		font-variant: normal;
		font-weight: normal;
		height: 93px;
		line-height: normal;
		margin-bottom: 8px;
		margin-left: 9.28px;
		margin-right: 9.28px;
		margin-top: 8px;
		min-height: 0px;
		min-width: 208px;
		outline-width: 0px;
		padding-bottom: 24px;
		padding-left: 24px;
		padding-right: 24px;
		padding-top: 24px;
		position: relative;
		text-align: center;
		text-transform: none;
		transition-delay: 0s;
		transition-duration: 0.28s;
		transition-property: box-shadow;
		transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
		width: 351.594px;
		z-index: 0;
		-webkit-user-select: none;
		}

		a:link, a:hover, a:visited, a:active {
			text-decoration: none;
		}
.hits {
  font-size: 2em;
  font-weight: bolder;
}

3. Finally, create a variable with initial value and get the span element by using JavaScript query selection. Use the on keyup function and check if key is a spacebar. The key code for spacebar is 32. So, use if statement to detect if spacebar has been pressed then call the addHit() function.

Define addHit() function and simply use increment operators to increase the hits variable value.

Add the following script between the <script> and </script> tag place it before the closing the body tag.

var hits = 0;
var hitElement = document.querySelector( '.hits' );
document.body.onkeyup = function(e) {
  if( e.keyCode == 32 ) {
    addHit();
  }
}

var addHit = function() {
  hits++;
  renderHits();
}

var renderHits = function() {
  hitElement.innerHTML = hits;
}

var resetHits = function() {
  hits = 0;
  renderHits();
}

That’s all! hopefully, you have successfully created Spacebar Hit Counter Javascript Code. If you have any questions or suggestions, feel free to comment below.

Similar Code Snippets:

  • Simple Vanilla JavaScript Tabs

    Simple Vanilla JavaScript Tabs

  • JavaScript Single Select Dropdown

    JavaScript Single Select Dropdown

  • Automatic Animated Image Slider in JavaScript

    Automatic Animated Image Slider in JavaScript

  • To Do List Project in JavaScript

    To Do List Project in JavaScript

  • Filter Table with Select Option in JavaScript

    Filter Table with Select Option in JavaScript

  • JavaScript Multiple Choice Questions Code

    JavaScript Multiple Choice Quiz Questions Code

  • Elastic Custom Cursor in Vanilla JavaScript

    Elastic Custom Cursor in Vanilla JavaScript

  • Next and Previous Buttons in JavaScript with Demo

    Next and Previous Buttons in JavaScript with Demo

  • Form Validation in JavaScript Code

    Form Validation in JavaScript Code

Asif Mughal

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.

Categories Vanilla JavaScript
Make Table Columns Resizable with jQuery
jQuery Easy Accordion Plugin – monica.js

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Categories

    • Accordion 22
    • Bootstrap 81
      • Bootstrap Accordion 1
      • Bootstrap Buttons 4
      • Bootstrap Cards 2
      • Bootstrap Carousel 2
      • Bootstrap Dropdowns 2
      • Bootstrap Forms 6
      • Bootstrap Jumbotron 1
      • Bootstrap Modals 2
      • Bootstrap Navbars 6
      • Bootstrap Panels 1
      • Bootstrap Progress Bars 1
      • Bootstrap Text & Input 3
    • Carousel 58
    • Chart & Graph 15
    • Date & Time 49
    • Gallery 39
    • HTML5 & CSS3 201
    • Layout 17
    • LightBox 19
    • Menu & Nav 95
    • Modal 18
    • Others 58
    • Portfolio 8
    • Social Media 10
    • Text & Input 120
    • Vanilla JavaScript 163
    • Zoom 12

You May Like

Alert (7) Analog Clock (4) Analog Clocks (5) Back to Top (4) Calculator (16) Calendar (5) Chart (3) Color Picker (4) Contact Us Forms (5) Countdown Timer (6) Datepicker (9) Digital Clocks (10) Drag and Drop (4) Dropdown Menu (23) File Uploader (4) Hamburger Menu (9) Horizontal Menu (24) Hover Effects (5) HTML5 Canvas (8) Image Sliders (40) Mega Menu (11) Modal (5) Multi-Level Menu (9) News Ticker (4) Notification (9) Off-Canvas Menu (10) Pagination (5) Parallax (8) Popup Lightbox (20) Progress Bar (5) Range Sliders (4) Scrolling Effects (28) Search Box (4) Select Dropdown (4) Shopping Cart (3) Side Menu (16) Slideshow (19) Social Share Buttons (3) Tabs (9) Text (11) Timeline (5) Timer (4) Tooltip (4) Typing Effect (6) Vertical Menu (14)

Latest Code Snippets

  • Common Open Source Security Vulnerabilities and How to Mitigate ThemCommon Open Source Security Vulnerabilities and How to Mitigate Them
    October 12, 2024
  • Animating Grid-column on Hover Using CSSAnimating Grid-column on Hover Using CSS
    April 1, 2024
  • Star Trails Animation On Mousemove in JavaScriptStar Trails Animation On Mousemove in JavaScript
    March 31, 2024
  • Gravity Button Using Pure CSSGravity Button Using Pure CSS
    March 31, 2024
  • Padlock Toggle Switch in CSSPadlock Toggle Switch in CSS
    March 31, 2024
  • Scroll-driven Animations in Pure CSSScroll-driven Animations in Pure CSS
    March 31, 2024
  • Interactive Notification Center UI in Vanilla JSInteractive Notification Center UI in Vanilla JS
    March 31, 2024
  • Emoji Spinner in Vanilla JavaScriptEmoji Spinner in Vanilla JavaScript
    March 31, 2024
  • Reveal Secret Code Using CSSReveal Secret Code Using CSS
    March 31, 2024
  • Toggle Button in CSS With Stretchable Elastic EffectToggle Button in CSS With Stretchable Elastic Effect
    March 31, 2024

Popular

  • 65+ Login Page in HTML with CSS Code Sample Simple to Difficult - 947,408 views
  • 25+ Best JavaScript Shopping Cart Examples with Demo - 211,942 views
  • Bootstrap Multiselect Dropdown with Checkboxes - 150,950 views
  • Bootstrap 5 Buttons with Icon and Text Tutorial & Demo - 112,360 views
  • 19+ Bootstrap Select Dropdown with Search Box Tutorial & Examples - 101,041 views
  • Bootstrap 5 Sidebar Menu with Submenu Collapse/Hover Tutorial Demo - 88,106 views
  • 99+ Social Media Buttons HTML Code Sample & Tutorial - 86,393 views
  • 19+ Bootstrap 5 Mega Menu Responsive/Drop Down Examples - 86,390 views
  • Bootstrap 4 Modal Popup Login Form Tutorial & Demo - 82,150 views
  • Bootstrap Vertical Menu with Submenu on Click - 80,202 views

Advertisement

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

CodeHim
  • About
  • Contact Us
  • Terms and Conditions
  • Privacy Policy
Top Categories
  • Bootstrap Snippets
  • JavaScript Snippets
  • HTML CSS Snippets
  • Menu & Nav Snippets
Get in Touch

Follow us on social media to be updated with latest web design code & scripts...

All Rights Reserved © 2026 - CodeHim Inc

Please Rel0ad/PressF5 this page if you can't click the download/preview link

F5
X