Web Development Kodex

gallery commission calculator

custom.js

'use strict'
const galleryPrice = document.getElementById('price')
const galleryPercentage = document.getElementById('percent')
const galleryCommission = document.getElementById('commission')
const artistsTotal = document.getElementById('total')
const btn = document.getElementById('calc')
function commissionCalc(price, percent) {
const total = price * (percent / 100)
return total
}
btn.addEventListener('click', () => {
const commission = commissionCalc(galleryPrice.value, galleryPercentage.value)
galleryCommission.innerHTML = Math.round(commission * 100) / 100
const artistsPrice = galleryPrice.value - commission
artistsTotal.innerHTML = artistsPrice.toFixed(2)
})

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Gallery Commission Calculator</title>
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>JavaScript Gallery Commission Calculator</h1>
</header>
<div class="wrapper">
<div class="container">
<div id="calculator">
<table>
<tr>
<td>Gallery Price: $</td>
<td>
<input id="price" type="text">
</td>
</tr>
<tr>
<td>Gallery Commission: %</td>
<td>
<input id="percent" type="text">
</td>
</tr>
<tr>
<td>Gallery Commission: $</td>
<td id="commission"></td>
</tr>
<tr>
<td>Artists Total: $</td>
<td id="total"></td>
</tr>
</table>
<button id="calc">Calculate</button>
</div>
</div>
<!-- .container-->
</div>
<!-- .wrapper-->
<footer>
<small><a href="https://opensource.org/licenses/MIT">MIT</a></small>
</footer>
<script src="custom.js"></script>
</body>
</html>

styles.css

body {
background: #eeeeee;
font-size: 16px;
font-family: "Trebuchet MS", Helvetica, sans-serif;
margin: 0;
}
a {
color: crimson;
text-decoration: none;
}
a:hover {
color: red;
}
/* Layout
------------------------------------------*/
header,
.container,
footer {
margin: 0 auto;
width: 88%;
max-width: 1080px;
}
.wrapper {
padding: 1em 0;
background: #ffffff;
}
/* Table
------------------------------------------*/
td:first-child {
text-align: right;
}
td {
padding: .5em 0 .5em .7em;
}
tr:last-child td:last-child {
border-top: 2px solid black;
}
#calculator {
max-width: 376px;
display: flex;
flex-direction: column;
align-items: flex-end;
}
input {
font-size: 1em;
background: linear-gradient(#eeeeee, #ffffff);
border-top: 2px solid #cccccc;
border-left: 2px solid #cccccc;
border-bottom: 2px solid #eeeeee;
border-bottom: 2px solid #eeeeee;
padding: .5em;
}
button {
font-size: 1em;
border: none;
background: linear-gradient(#ffffff, #eeeeee);
outline: 0;
padding: .5em 1em;
border-radius: 15px;
text-shadow: 1px 1px 3px rgba(198, 198, 198, 0.9);
border: 2px solid #eeeeee;
}
button:hover {
background: linear-gradient(#eeeeee, #ffffff);
}