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">
	<h2>Cart</h2>
	<ul>
		<li>
			<div class="item-container">
				<div class="item-details">
					Apples
				</div>
				<div class="quantity-container">
					<button class="decrease">-</button>
					<span class="quantity">4</span>
					<button class="increase">+</button>
				</div>
			</div>
		</li>
		<li>
			<div class="item-container">
				<div class="item-details">
					Mangoes
				</div>
				<div class="quantity-container">
					<button class="decrease">-</button>
					<span class="quantity">5</span>
					<button class="increase">+</button>
				</div>
			</div>
		</li>
		<li>
			<div class="item-container">
				<div class="item-details">
					Bananas
				</div>
				<div class="quantity-container">
					<button class="decrease">-</button>
					<span class="quantity">6</span>
					<button class="increase">+</button>
				</div>
			</div>
		</li>
	</ul>
</div>
              
            
!

CSS

              
                /* This CSS Section is just for styling and is not related to the concept explained in the blog. Feel free to skip this section. */
* {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}

.container {
	border-radius: 5px;
	background-color: rgb(230, 230, 240);
	padding: 20px;
	max-width: 460px;
	margin: auto;
}

li {
	list-style: none;
	padding: 15px 20px;
	border-bottom: 2px solid #417ff4;
	border-radius: 5px;
}

.item-container {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.quantity {
	font-size: 1.2em;
	padding: 5px 10px;
}

button {
	padding: 5px 10px;
}

              
            
!

JS

              
                const ul = document.querySelector("ul");

ul.addEventListener("click", clickHandler);

function clickHandler(e) {
	// finding the button which was clicked
	const button = e.target;

	// finding the parent element of the button
	const buttonContainer = button.parentElement;

	// finding the quantity of the item for which the button was clicked
	// buttonContainer is a div with 3 elements in the following order
	// 1. decrease button, 2. quantity element (span), 3. increase button
	const quantityElement = buttonContainer.children[1]; // since the index starts with 0, hence quantityElement is addressed by 1 index
	const currentQuantity = Number(quantityElement.innerText); // converting innerText value to numerical value, by default it is string

	// changing the quantity based on the -/+ button clicked
	switch (e.target.className) {
		case "increase":
			quantityElement.innerText = currentQuantity + 1;
			break;
		case "decrease":
			quantityElement.innerText = currentQuantity === 0 ? 0 : currentQuantity - 1;
			break;
	}
}

              
            
!
999px

Console