17
loading...
This website collects cookies to deliver better user experience
if you want to follow me this is my code you can also check codepen above for the rest
<body>
<div class="container">
<div class="product">
<!-- add an image here or whatever -->
</div>
<div class="pricing">
<p>
<span class="price">100</span>
<span class="currency">$</span>
</p>
<div class="custom">
<button class="btn add">+</button>
<button class="btn remove">−</button>
</div>
<div class="add-to-cart">
<p>
<span class="quantity">1</span>
item to buy
</p>
<button class="buy-btn">Add To Card</button>
</div>
</div>
</div>
</body>
product
and pricing
divsdocument.querySelect....
is so silly, let's build a function to do that for us and re-use it whenever we want_
or $
(jQuery) or whatever you want to call it but make it shorter because that's the reason why we creating it xDfunction _s(element, all) { //_ used for private functions but don't think of that now just keep it simple
if(all) {
return document.querySelectorAll(element)
}
return document.querySelector(element);
}
truthy
value if you want to use the querySelectorAll()
function, else ... leave it;querySelector()
function returns a single html element and you can use all the methods directly on it, the querySelectorAll()
returns an array (nodeList) so you can only use arrays methods on it,to access the elements you have to loop through it,_s()
functionlet price = _s(".price").textContent,
//the text that shows how mush money we need spend
buttons = _s(".btn", true),
//array of buttons with the class .btn [add and remove buttons]
quantity = _s(".quantity"),
//the number of items that we will buy (string)
items = parseInt(quantity.textContent);
//transfer the quantity using the parseInt function
//with a single parameter it returns an integer number type
//if it can't transfer it it returns NaN
forEach()
methode to loop over the numbers learn more about it here ... you can use any other methodsbuttons.forEach(button => {
button.addEventListener("click", function(e) {
})
})
addEventListener
;e.target
, this will return the html element after the event finished,+
or -
button.class
and check the class of that target element (look at the html)classList
method that return an array of element's classes then check if it contains add
or remove
class to know what to do in each case.function _s(element, all) {
if(all) {
return document.querySelectorAll(element)
}
return document.querySelector(element);
}
let price = _s(".price").textContent,
buttons = _s(".btn", true),
quantity = _s(".quantity"),
items = parseInt(quantity.textContent);
buttons.forEach(button => {
button.addEventListener("click", function(e) {
if(e.target.classList.contains("add")) {
} else { //we have only two buttons so if it's not "add" it will be "remove"
}
})
})
price * items
, since the price is a textContent (string) we have to convert it to a valid number, we can use the parseInt()
as we learned before or 😎 use this trick(Idk why I wrote the emoji but I just learned it too), using + before a string number will convert it to a number+price * items
function _s(element, all) {
if(all) {
return document.querySelectorAll(element)
}
return document.querySelector(element);
}
let price = _s(".price").textContent,
buttons = _s(".btn", true),
quantity = _s(".quantity"),
items = parseInt(quantity.textContent);
buttons.forEach(button => {
button.addEventListener("click", function(e) {
if(e.target.classList.contains("add")) {
items++
} else {
if(items > 1) items--
}
_s(".price").textContent = +price * items;
quantity.textContent = items;
})
})