xxxxxxxxxx
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Two Numbers</title>
</head>
<body>
<h1>Addition of Two Numbers in Javascript</h1>
<label for="num1" class="input-label">Number 1:</label>
<input type="text" id="num1" class="input-field">
<label for="num2" class="input-label">Number 2:</label>
<input type="text" id="num2" class="input-field">
<button onclick="addNumbers()" class="action-button">Add</button>
<p id="result" class="result-text">Result: </p>
<script>
function addNumbers() {
// Get values from textboxes
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
// Check if the input is valid
if (!isNaN(num1) && !isNaN(num2)) {
// Perform addition
var sum = num1 + num2;
// Display the result
document.getElementById("result").innerText = "Result: " + sum;
} else {
// Handle invalid input
alert("Please enter valid numbers");
}
}
</script>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
.input-label {
font-size: 16px;
margin-bottom: 8px;
}
.input-field {
padding: 8px;
margin-bottom: 16px;
width: 200px;
border: 1px solid #ccc;
border-radius: 4px;
}
.action-button {
background-color: #4caf50;
color: white;
padding: 10px 15px;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.action-button:hover {
background-color: #45a049;
}
.result-text {
font-size: 18px;
margin-top: 16px;
}
</style>
</body>
</html>