Energy Consumption Tool Create Using JavaScript With HTML

Hello Friends Today, through this tutorial, I will tell you How to simple Energy Consumption Tool created using JavaScript and HTML? Sure, here’s a simple Energy Consumption Tool created using JavaScript and HTML. This tool calculates energy consumption based on power consumption (in kW) and time (in hours).

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Energy Consumption Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 16px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>Energy Consumption Calculator</h2>
<label for="power">Enter Power Consumption (kW):</label>
<input type="number" id="power" placeholder="Enter power consumption"> 
<label for="time">Enter Time (Hours):</label>
<input type="number" id="time" placeholder="Enter time in hours">
<button onclick="calculateConsumption()">Calculate Consumption</button>
<p id="result"></p>

<script>
function calculateConsumption() {

// Get the input values
const power = parseFloat(document.getElementById('power').value);
const time = parseFloat(document.getElementById('time').value);

// Calculate energy consumption (kWh)
const consumption = power * time;

// Display the result
document.getElementById('result').innerHTML = `Energy Consumption: ${consumption.toFixed(2)} kWh`;
}
</script>
</body>
</html>

This HTML file includes input fields for entering power consumption (in kW) and time (in hours), along with a button to trigger the calculation. When the button is clicked, the `calculateConsumption` function is called, which calculates the energy consumption (in kWh) by multiplying power consumption by time. Finally, it displays the result on the page.