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

              
                

<h1>Habit <span>X <span id="maxCells"></span></span> Calendar</h1>

<div class="grid">
	
</div>

<div class="grid-controls">
	<label for="cols">Columns</label>
	<input type="number" id="cols" min="1" max="10" value="10">
	<label for="rows">Rows</label>
	<input type="number" id="rows" min="1" max="9" value="9"> 
	
	<button>Click to Print</button>
</div>

              
            
!

CSS

              
                body {
	display: grid;
	justify-content: center;
	padding-bottom: 3em;
}

h1 {
	text-align: center;
	padding-top: 50px;
	margin-bottom: 50px;
	
	
	span {
		color: red;
	}
}

button {
	display: inline-block;
	width: 150px;
	margin-left: 20px;
}

.grid {
	--cols: 10;
	--rows: 9;
	
	padding-left: 1em;
	padding-right: 1em;
	display: grid;
	grid-template-columns: repeat(var(--cols), 50px);
	grid-template-rows: repeat(var(--rows), 50px);
	grid-gap: 1em;
	padding-bottom: 100px;
}

.cell {
	border: 1px solid black;
	cursor: pointer;
}

input {
	margin-bottom: 1em;
}

.grid-controls {
	position: absolute;
	width: 100%;
	text-align: center;
}

.cell--active {
	position: relative;
	
	&::before, 
	&::after {
		transform: rotate(45deg);
		height: 90%;
		width: 3px;
		content: "";
		display: block;
		background-color: red;
		top: calc(5% - 1px);
		left: calc(50% - 1px);
		position: absolute;
	}
	
	&::after {
		transform: rotate(-45deg);
	}
}

@media print {
	
	h1 span,
	.grid-controls {
		display: none;
	}
	
	
	.cell--active {
		border-color: black;
		background-color: transparent;

		&::before, 
		&::after {
			background-color: black;
		}
	}
}


              
            
!

JS

              
                // Inspired by https://www.thedolectures.com/products/habit-calendar
const grid = document.querySelector('.grid');
const cell = '<div class="cell"></div>';
const ROWS = 9;
const COLUMNS = 10;
const TOTAL_CELLS = COLUMNS * ROWS;

let cols = document.querySelector('#cols').value;
let rows = document.querySelector('#rows').value;

let totalCells = 0; 
let maxCells = TOTAL_CELLS;

const addInitialHTML = () => {
	let html = '';
	for (let i = 0; i < TOTAL_CELLS; i++) {
	   html += cell;
	}
	grid.innerHTML = html;	
};
addInitialHTML();





var cells = document.querySelectorAll('.cell');

const updateColsandRows = function(e) {
	if ('cols' === e.target.id) {
		cols = e.target.value;
		grid.style.setProperty('--cols', cols);
		localStorage.setItem('columns', cols);
	} else {
		rows = e.target.value;
		grid.style.setProperty('--rows', rows);
		localStorage.setItem('rows', rows);
	}

	let index = 0;
	maxCells = cols * rows;
	document.querySelector('#maxCells').innerHTML = maxCells;
	for (index; index < TOTAL_CELLS; index++) {
		if (index >= maxCells) {
			cells[index].style.display = 'none';		
		} else {
			cells[index].style.display = 'block';		
		}
	}
	
};

[...document.querySelectorAll('input')]
	.forEach(input => {
		// input.addEventListener('change', updateColsandRows);
		input.addEventListener('input', updateColsandRows);
	});

var cellTracking = document.querySelectorAll('.cell');

// Track the clicks on the days / cells
// Create empty array with 90 entries as zero.
var selectedCells = localStorage.getItem('cells');


// Check if there is a cells in localStorage.
if (typeof selectedCells === null) {
	const cellArray = [];
	
	for (let i = 0; i < totalCells; i++) {
		cellArray.push(0);
	}
	localStorage.setItem("cells", cellArray);
	localStorage.setItem("columns", COLUMNS);
	localStorage.setItem("rows", ROWS);

} else {
	console.log({selectedCells})
	if (selectedCells) {
		// on page load, go through localStorage 'cells' and add class to those ticked off.
		const selectedArray = selectedCells.split(',');
		totalCells = 0;
		for (var i = 0, len = selectedArray.length; i < len; i++) {
			if (selectedArray[i] === '1') {
				cellTracking[i].classList.add('cell--active');
				totalCells++;
			}
		}

		const columns = localStorage.getItem('columns');
		const rows = localStorage.getItem('rows');
		grid.style.setProperty('--cols', columns);
		grid.style.setProperty('--rows', rows);

		maxCells = rows * columns;
		let index = 0;
		for (index; index < TOTAL_CELLS; index++) {
			if (index >= maxCells) {
				cells[index].style.display = 'none';		
			} else {
				cells[index].style.display = 'block';		
			}
		}

		// update the inputs with the column/row values;
		const inputs = document.querySelectorAll('input');
		for (let input of inputs) {
			if ('cols' === input.id) {
				input.value = columns;
			} else {
				input.value = rows;
			}
		}
	}
}

const handleCellClick = (index) => {
	const cell = document.querySelector(`.cell:nth-child(${index + 1})`);
	
	cell.classList.toggle('cell--active');
	
	var selected = localStorage.getItem('cells') ?? "";
	const selectedArray = selected.split(',');
	
	var activeString = '0';
	if (cell.classList.contains('cell--active')) {
		activeString = '1';
		totalCells++;
	} else {
		totalCells--;
	}
	selectedArray.splice(index, 1, activeString);
	
	localStorage.setItem('cells', selectedArray);
	
	updateScore(totalCells);
};

const updateScore = (score) => {
	const span = document.querySelector('h1 span');
	span.innerHTML = `${score} / <span id="maxCells">${maxCells}</span>`;
}

// Handle Clicks on the cells
[...cellTracking].forEach((cell, index) => cell.addEventListener('click', handleCellClick.bind(null, index)));


document.querySelector('button').addEventListener('click', function() {
	window.print();
});
              
            
!
999px

Console