Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container-fluid" id="container">
	<form>

		<div class="row" id="header">
			<div class="col-12">
				<h1 class="text-center">Tip Calculator</h1>
			</div>
		</div>

		<div class="row" id="bill-section">
			<div class="col-12">
				<h3>Bill amount</h3>
			</div>

			<div class="col-12">
				<input id="bill-input" type="text" placeholder="$0.00" maxlength="8" inputmode="numeric">
				<!--SET CHARACTER LIMITS-->
			</div>
		</div>

		<div class="row" id="tip-section">
			<div class="col-12">
				<h3>How was the service?</h3>
			</div>

			<div class="col-12">
				<div class="row" id="tip-button-section">
					<div class="col-md col-12">
						<div class="tip-buttons" id="tip-not-great" data-value="10">10%</div>
					</div>
					<div class="col-md col-12">
						<div class="tip-buttons" id="tip-average" data-value="15">15%</div>
					</div>
					<div class="col-md col-12">
						<div class="tip-buttons" id="tip-great" data-value="18">18%</div>
					</div>
					<div class="col-md col-12">
						<div class="tip-buttons" id="tip-excellent" data-value="20">20%</div>
					</div>
					<div class="col-md col-12">
						<div class="tip-buttons custom" id="tip-custom">
							<span class="custom" id="tip-custom-text">Custom</span>
							<span class="custom" id="tip-custom-input"><input class="custom" type="text" maxlength="3" inputmode="numeric">%</span>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="row" id="split-section">
			<div class="col-1"></div>
			<div class="col-10">
				<label for="split-toggle-box">
					<input id="split-toggle-box" type="checkbox" name="split-toggle-box"> Split the check?
				</label>
			</div>
			<div class="col-1"></div>

			<div class="col-1"></div>
			<div class="col-10">
				<span id="split-prompt">
					Split the check between
					<select id="split-selection">
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7</option>
						<option value="8">8</option>
					</select>
					people
				</span>
			</div>
			<div class="col-1"></div>
		</div>

		<div class="row" id="totals-section">
			<div class="col-12 col-md-6">
				<h4 class="text-center">Your tip</h4>
				<p class="text-center" id="tip-output">$0.00</p>
			</div>
			<div class="col-12 col-md-6">
				<h4 class="text-center">Total due</h4>
				<p class="text-center" id="total-output">$0.00</p>
			</div>
		</div>

	</form>
</div>
<div class="container-fluid" id="footer">
	<div class="row">
		<div class="col-12">
			<p class="text-center"><em>yanssel.com &copy; 2024</em></p>
		</div>
	</div>
</div>
              
            
!

CSS

              
                //Text variables
$font-family: 'Varela Round', sans-serif;
$main-title-size: 30px;
$normal-title-size: 24px;
$normal-text-size: 20px;

//Color variables
$primary-default: #152B42;
$primary-light: #1159A6;
$primary-dark: #152B42;

$primary-default-text: black;
$primary-light-text: black;
$primary-dark-text: white;

$bill-text: #152B42;

//General
body {
	font-family: $font-family;
	font-size: $normal-text-size;
}
h1 {
	font-size: $main-title-size;
	font-weight: 800;
	letter-spacing: 1px;
}
h3 {
	font-size: $normal-title-size;
}

//Header
#header {
	//Layout
	position: relative;
	height: 10vh;
	min-height: 50px;
	
	//Design
	color: $primary-dark-text;
	background-color: $primary-dark;
	
	h1 {
		line-height: max(10vh, 50px);
	}
}

//Bill section
#bill-section {
	//Layout
	position: relative;
	margin-top: 5vh;
	
	input {
		//Layout
		position: relative;
		width: 100%;
		height: 8vh;
		min-height: 50px;
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
		
		//Text
		text-align: center;
		font-size: 30px;
		color: $bill-text;
		
		//Design
		border-radius: 15px;
		border: 2px solid;
		border-color: gray;
		outline: none;
	}
}

//Tip section
#tip-section {
	//Layout
	position: relative;
	margin-top: 5vh;
}

.tip-buttons {
	//Layout
	position: relative;
	height: 6vh;
	min-height: 25px;
	margin-top: 4px;
	margin-bottom: 4px;
	
	//Text
	text-align: center;
	color: $primary-dark-text;
	line-height: max(6vh, 25px);
	
	//Design
	border-radius: 15px;
	background-color: $primary-light;
}

.tip-buttons:hover {
	//Design
	cursor: pointer;
	
	//Animation
	animation-name: tip-back;
	animation-duration: 250ms;
	animation-fill-mode: forwards;
}

@keyframes tip-back {
	100% {
		color: $primary-dark-text;
		background-color: $primary-dark;
	}
}

#tip-custom-input {
	//Layout
	position: relative;
	display: none;
	
	input {
		//Layout
		position: relative;
		width: 40px;
		min-height: 20px;
		height: 4vh;
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
		
		//Text
		text-align: right;
		
		//Design
		outline: none;
		color: white;
		background-color: $primary-dark;
		border-top: 0;
		border-left: 0;
		border-right: 0;
		border-bottom: 1px solid;
		border-color: white;
	}
}

//Split section
#split-section {
	//Layout
	position: relative;
	margin-top: 5vh;
	
	//Text
	text-align: center;
}

#split-prompt {
	//Layout
	display: none;
	
	select {
		//Layout
		position: relative;
		
		//Design
		border-left: 0;
		border-right: 0;
		border-top: 0;
		border-bottom: 1px solid;
		outline: 0;
		background-color: white;
	}
}

//Totals section
#totals-section {
	//Layout
	position: relative;
	margin-top: 5vh;
	margin-bottom: 10vh;
	
	h4 {
		color: $primary-dark;
	}
}

//Footer
#container {
	//Layout
	position: relative;
	min-height: 100vh;
	margin-bottom: -50px;
}

#footer {
	//Layout
	position: relative;
	width: 100%;
	height: 50px;
	bottom: 0;
	
	//Text
	font-size: 14px;
	
	//Design
	background-color: $primary-default;
	
	.col-12 {
		//Layout
		position: relative;
		min-height: 50px;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	
	p {
		margin: 0;
	}
	
	a {
		//Text
		text-decoration: underline;
		
		//Design
		color: white;
	}
}
              
            
!

JS

              
                /*Set DOM objects to variables*/
const billInput 			= document.querySelector('#bill-input'); 				//The bill input box
const tipNotGreat 		= document.querySelector('#tip-not-great'); 		//The 'not great' tip button
const tipAverage 			= document.querySelector('#tip-average');				//The 'average' tip button
const tipGreat 				= document.querySelector('#tip-great');					//The 'great' tip button
const tipExcellent 		= document.querySelector('#tip-excellent');			//The 'excellent' tip button
const tipCustom 			= document.querySelector('#tip-custom');				//The 'custom' tip button
const tipCustomInput	= document.querySelector('#tip-custom-input');	//The custom tip input box
const tipButtons 			= document.querySelectorAll('.tip-buttons');		//The node list of all tip buttons
const splitToggleBox	= document.querySelector('#split-toggle-box');	//The splitting checkbox
const splitPrompt			= document.querySelector('#split-prompt');			//The split selection prompt
const splitSelection 	= document.querySelector('#split-selection');		//The selection box for splitting the check
const tipOutput 			= document.querySelector('#tip-output');				//The tip output box
const totalOutput 		= document.querySelector('#total-output');			//The total output box

/*Set color constants*/
const primaryDefault 	= 'rgb(21, 43, 66)';
const primaryLight 		= 'rgb(17, 89, 166)';
const primaryDark 		= 'rgb(21, 43, 66)';

//Set initial tip button styling
tipAverage.style.color = 'white';
tipAverage.style.backgroundColor = primaryDark;

/*Tip calculator object*/
let tipCalculator = {
	//Values
	bill: 0,
	tipPercent: 15,
	split: 1,
	tip: 0,
	total: 0,
	
	//Getters
	getBill: function() {
		console.log(this.bill);
		return this.bill;
	},
	getTipPercent: function() {
		console.log(this.tipPercent);
		return this.tipPercent;
	},
	getTip: function() {
		console.log(this.tip);
		return this.tip;
	},
	getTotal: function () {
		console.log(this.total);
		return this.total;
	},
	
	//Setters
	setBill: function(newBill) {
		this.bill = (Math.round(newBill * 100)) / 100;
		this.recalculate(); //Recalculate tip and total
		/*console.log("Bill: " + this.bill);
		console.log("Percent: " + this.tipPercent);
		console.log("Split: " + this.split);
		console.log("Tip: " + this.tip);
		console.log("Total: " + this.total);*/
	},
	setTipPercent: function(newTipPercent) {
		this.tipPercent = newTipPercent;
		this.recalculate(); //Recalculate tip and total
		/*console.log("Bill: " + this.bill);
		console.log("Percent: " + this.tipPercent);
		console.log("Split: " + this.split);
		console.log("Tip: " + this.tip);
		console.log("Total: " + this.total);*/
	},
	setSplit: function(newSplit) {
		this.split = newSplit;
		this.recalculate();
		/*console.log("Bill: " + this.bill);
		console.log("Percent: " + this.tipPercent);
		console.log("Split: " + this.split);
		console.log("Tip: " + this.tip);
		console.log("Total: " + this.total);*/
	},
	
	//Calculator
	recalculate: function() { //Function to recalculate tip and total
		//Recalculate
		this.tip = Math.round(this.bill * this.tipPercent) / 100;
		this.total = Math.round((this.bill + this.tip) * 100) / 100;
		
		//Display totals
		let splitTip = Math.round((this.tip * 100) / this.split) / 100;
		let splitTotal = Math.round((this.total * 100) / this.split) / 100;
		tipOutput.textContent = '$' + splitTip.toFixed(2);
		totalOutput.textContent = '$' + splitTotal.toFixed(2);
	}
};

/*Functions*/
let generateListeners = () => { //Function to generate all event listeners
	billInput.addEventListener('input', cleanBillInput);
	billInput.addEventListener('change', submitBillInput);
	tipCustomInput.addEventListener('input', cleanCustomInput);
	tipCustomInput.addEventListener('change', submitCustomInput);
	splitToggleBox.addEventListener('change', toggleSplit);
	splitSelection.addEventListener('change', updateSplit);
	
	for (let i = 0; i < tipButtons.length; i++) {
		tipButtons[i].addEventListener('click', changeTip);
	}
};

let cleanBillInput = (e) => { //Keep bill input in the following format: $0.00
	//Grab input
	let inputValue = e.target.value;
	let inputValueArr = inputValue.split(''); 
	
	//Store digits only
	let numberTest = /\d/;
	let numberArr = inputValueArr.filter(char => numberTest.test(char));
	
	//Ensure array is not empty (push a 0 if it is)
	if (numberArr.length === 0) { numberArr.push('0'); }
	
	//Remove excess zeroes
	let cleanArr = parseInt(numberArr.join('')).toString().split('');
	
	//Generate return value
	let cleanValue = '$0.00';
	if (cleanArr.length === 1) {
		cleanValue = '$0.0' + cleanArr[0];
	} else if (cleanArr.length === 2) {
		cleanValue = '$0.' + cleanArr[0] + cleanArr[1];
	} else if (cleanArr.length === 3) {
		cleanValue = '$' + cleanArr[0] + '.' + cleanArr[1] + cleanArr[2];
	} else {
		cleanValue = cleanArr
			.concat([])
			.reverse()
			.map((char, ind) => {
				return ind === 1 ? '.' + char : ind === cleanArr.length - 1 ? '$' + char : char;
			})
			.reverse()
			.join('');
	}
	
	//Output clean value
	e.target.value = cleanValue;
};

let submitBillInput = (e) => { //Change bill amount
	//Grab input
	let inputValue = e.target.value;

	//Remove $ from input and turn into a number
	let stringBillValue = inputValue
		.split('')
		.filter(char => char != '$')
		.join('');
	let numberBillValue = parseFloat(stringBillValue);
	
	//Submit new bill value for calculation
	tipCalculator.setBill(numberBillValue);
};

let changeTip = (e) => {
	//Grab button 
	let button = e.target;
	if (button.className !== 'tip-buttons') { button = tipCustom; } //Ensure we capture the appropriate button (and not its children)

	//Reset all button colors
	for (let i = 0; i < tipButtons.length; i++) { 
		tipButtons[i].style.color = 'white';
		tipButtons[i].style.backgroundColor = primaryLight; 
	}
	
	//Set selected button styling
	button.style.color = 'white';
	button.style.backgroundColor = primaryDark;
	
	//Grab the custom button's children
	let customText = tipCustom.firstChild.nextSibling;
	let customInput = tipCustom.lastChild.previousSibling;
	
	if (e.target.classList.contains('custom')) { //Check to see whether the user is aiming to submit a custom tip
		//Reveal custom input box
		customText.style.display = 'none';
		customInput.style.display = 'block';
		customInput.firstChild.focus(); //Focus specifically on the <input> tag itself
	} else {
		//Hide custom input, in case it's active, and wipe input value
		customText.style.display = 'block';
		customInput.style.display = 'none';
		customInput.firstChild.value = ''; //Reset the value of the <input> tag itself
		
		//Clean and submit the tip
		let tipCheck = Math.abs(parseInt(e.target.getAttribute('data-value'))); //Turn data-value into an integer and get the absolute value to disallow negative values
		let tipValue = typeof tipCheck === 'number' ? tipCheck : 15; //Only save value if it's a number, otherwise default to 15
		tipCalculator.setTipPercent(tipValue);
	}
};

let cleanCustomInput = (e) => {
	//Grab custom tip input
	let customInputValue = e.target.value;
	
	//Only allow for numbers
	let numberTest = /\d/;
	let cleanValue = customInputValue
		.split('')
		.filter(char => numberTest.test(char))
		.join('');
	
	//Display clean input
	e.target.value = cleanValue;
};

let submitCustomInput = (e) => {
	//Clean and submit custom tip
	let tipCheck = Math.abs(parseInt(e.target.value)); //Turn value into an integer and get the absolute value to disallow negative values
	let tipValue = tipCheck > -1 ? tipCheck : 15; //Only save value if it's a number, otherwise default to 15
	tipCustomInput.firstChild.value = tipValue;
	tipCalculator.setTipPercent(tipValue);
};

let toggleSplit = (e) => {
	//Grab checkbox state
	let checked = splitToggleBox.checked;
	
	//Toggle display and set initial split value
	if (checked === true) {
		//Make prompt visible
		splitPrompt.style.display = 'inline';
		
		//Set split value to 2
		tipCalculator.setSplit(2);
	} else {
		//Hide prompt
		splitPrompt.style.display = 'none';
		
		//Set split value to 1
		tipCalculator.setSplit(1);
		
		//Reset selection
		splitSelection.value = '2';
	}
};

let updateSplit = (e) => {
	//Grab split value
	let splitInput = e.target.value;
	
	//Make string value numeric
	let splitValue = parseInt(splitInput);
	
	//Update split value
	tipCalculator.setSplit(splitValue);
};

/*Generate event listeners*/
generateListeners();
              
            
!
999px

Console