A Pen by Stefan Hilger

Thumbnail
This awesome code was written by megatronCGN, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright megatronCGN ©
  • HTML
  • CSS
  • JavaScript
    <button class="js-new-row">New Row</button>
<div class="grid js-grid-container"></div> 

/*Downloaded from https://www.codeseek.co/megatronCGN/a-pen-by-stefan-hilger-pLYLOP */
    .grid__row {
	min-height: 100px;
	background-color: green;
}

.grid__item {
	min-width: 100px;
	min-height: 100px;
	display: inline-block;
	background-color: pink;
}


/*Downloaded from https://www.codeseek.co/megatronCGN/a-pen-by-stefan-hilger-pLYLOP */
    (function() {
	
	var $createRow = $('.js-new-row');
	var $createItem = $('.js-new-item');
	var $gridContainer = $('.js-grid-container');
	var rowId = 0;
	var itemId = 0;
	var gridMarkup = '<div class="grid__row"></div>';
	var gridItemMarkup = '<div class="grid__item"></div>';
	var createItemButtonMarkup = '<button class="js-new-item">New Item</button>';
	
	function createRow($container) {		
		$container.append(gridMarkup);
		var $row = $container.children('.grid__row').last();
		$row.attr('id', rowId);
		rowId = rowId++;
		return $row;
	}
	
	function createItem($row, itemId) {
		$row.append(gridItemMarkup);
		var $item = $row.children('.grid__item').last();
		$item.attr('id', $row.attr('id') + '-' + itemId);
		itemId =  itemId++;		
		$row.find('button').remove();
		$row.append(createItemButtonMarkup);
		var $button = $row.children('button').last();
	};
	
	$createRow.click(function() {
		var $newRow = createRow($gridContainer);
		createItem($newRow);
	});
	
	$createItem.on('click',function() {
		createItem($(this).parent());
	});
	
}());

Comments