Complete working code of Dividend Calculator Tool Code. Tested and verified, you just need to copy and paste the code and enjoy.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dividend Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(to right, #6a11cb, #2575fc);
color: white;
padding: 20px;
}
.calculator {
background: white;
color: black;
border-radius: 10px;
padding: 20px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
}
.btn-custom {
background-color: #6a11cb;
color: white;
}
.btn-custom:hover {
background-color: #2575fc;
}
.result {
background: #f0f8ff;
color: black;
border-radius: 5px;
padding: 15px;
}
@media (max-width: 768px) {
.calculator {
margin-top: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mb-4">Dividend Calculator</h1>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="calculator">
<form id="dividendForm">
<div class="mb-3">
<label for="stockPrice" class="form-label">Stock Price (in $)</label>
<input type="number" class="form-control" id="stockPrice" placeholder="Enter stock price" required>
</div>
<div class="mb-3">
<label for="annualDividend" class="form-label">Annual Dividend (in $)</label>
<input type="number" class="form-control" id="annualDividend" placeholder="Enter annual dividend" required>
</div>
<div class="mb-3">
<label for="sharesOwned" class="form-label">Number of Shares Owned</label>
<input type="number" class="form-control" id="sharesOwned" placeholder="Enter number of shares owned" required>
</div>
<button type="submit" class="btn btn-custom w-100">Calculate</button>
</form>
<div id="result" class="result mt-4" style="display: none;">
<h5>Results:</h5>
<p><strong>Dividend Yield:</strong> <span id="dividendYield"></span>%</p>
<p><strong>Annual Dividend Income:</strong> $<span id="annualIncome"></span></p>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('dividendForm').addEventListener('submit', function (event) {
event.preventDefault();
const stockPrice = parseFloat(document.getElementById('stockPrice').value);
const annualDividend = parseFloat(document.getElementById('annualDividend').value);
const sharesOwned = parseInt(document.getElementById('sharesOwned').value);
if (stockPrice > 0 && annualDividend > 0 && sharesOwned > 0) {
const dividendYield = ((annualDividend / stockPrice) * 100).toFixed(2);
const annualIncome = (annualDividend * sharesOwned).toFixed(2);
document.getElementById('dividendYield').textContent = dividendYield;
document.getElementById('annualIncome').textContent = annualIncome;
document.getElementById('result').style.display = 'block';
} else {
alert('Please enter valid numbers greater than zero.');
}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>