Open In App

Create a Build A Simple Alarm Clock in HTML CSS & JavaScript

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will develop an interactive simple Alarm Clock application using HTML, CSS, and JavaScript languages.

Develop an alarm application featuring date and time input fields for setting alarms. Implement comprehensive validation to prevent duplicate alarms and enforce a maximum limit of three alarms per user. Ensure alarms can only be set for future dates and times, triggering pop-up notifications when they occur.

Preview of final output: Let us have a look at how the final output will look like.

Screenshot-2023-10-04-at-14-44-25-Advanced-Alarm-Clock-min

Prerequisites:

Approach:

  • Create the HTML layout structure using the different tags. We have used the h1 and h3 tags to show the GeeksforGeeks title and name of the application. Then we used the <div> class to display the ongoing time to the user. There is <input> of type date to display the date and <input> type time to take the time as an input. There is <button> to trigger the alarm set action.
  • In this styling file, all the styling properties and colors are been specified. Entire colors, padding, and effects are been managed through this file.
  • In this JavaScript code, we first store the reference of HTML elements like 'time', 'alarmDate' etc.
  • Then we have a user-defined function as timeChangeFunction() which is used to show the current running time on screen to the user.
  • There is alarmSetFunction() which is used to set the alarm for the time the user has been entered as an input. Also, the validation is also done here at like same time and cannot have 2 alarms, more than 3 alarms are not allowed.
  • In the showAlarmFunction() the alarms that are set by the user are been shown here, the user can also delete the alarm as per the need.

Steps to Create the Application:

Step 1: Open the VSCode or any other IDE as per your interest.

Step 2: After opening IDE on your local machine create three files index.html, styles.css, and script.js.

Step 3: Write the code of JavaScript in your script file, CSS code in your styles file, and HTML code in the index file.

Example: This example describes the basic implementation of the Simple Alarm Clock application using HTML, CSS, and Javascript.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport"
		content="width=device-width, 
				initial-scale=1.0">
	<link rel="stylesheet" href="style.css">
	<title>Advanced Alarm Clock</title>
</head>

<body>
	<div class="clock">
		<h1>
			GeeksforGeeks
		</h1>
		<h3>
			A Simple Alarm Clock in
			HTML CSS & JavaScript
		</h3>
		<div class="time" id="time">
			00:00:00
		</div>
		<div class="input-row">
			<div class="input-field">
				<label for="alarmDate">
					Select Date:
				</label>
				<input type="date"
					id="alarmDate"
					class="alarm-input"
					min="">
			</div>
			<div class="input-field">
				<label for="alarmTime">
					Select Time:
				</label>
				<input type="time" id="alarmTime"
					class="alarm-input">
			</div>
			<button id="setAlarm">
				Set Alarm
			</button>
		</div>
		<div class="alarms" id="alarms">
		</div>
	</div>
	<script src="script.js"></script>
</body>

</html>
CSS JavaScript

Step s to run the Application: Open the live server and search the local host URL in your browser

http://localhost:5500/

Output:


Next Article

Similar Reads