As a newcomer to web development, I took on the challenge of creating an e-commerce website using only HTML, CSS, and vanilla JavaScript (without any frameworks).
I've encountered a roadblock while trying to implement the "Add to Cart" functionality for my products. Below is a snippet of my code:
<div class="small-container">
<div class="row">
<div class="col-4">
<img src="images/20 ps4.png" alt=""/></a>
<h4>20 Playstation Store(PSN)</h4>
<p>$20.00</p>
<button type="button" class="btn1" ><h3 class="text-btn">add to cart</h3></button>
</div>
<div class="col-4">
<img src="images/25 ps4.png" alt="" />
<h4>25 Playstation Store(PSN)</h4>
<p>$25.00</p>
<button type="button" class="btn1" onclick="window.location.href='cart.html'"><h3 class="text-btn">add to cart</h3></button>
</div>
</div>
For the JavaScript part:
function ready() {
var removeCartItemButtons = document.getElementsByClassName('btn1')
for (var i = 0; i < removeCartItemButtons.length; i++) {
var button = removeCartItemButtons[i]
button.addEventListener('click', removeCartItem)
}
// Continue with other JavaScript functions...
}
In terms of styling with CSS:
.row {
margin-top: 50px;
display: flex;
align-items: center;
flex-wrap: wrap;
justify-content: space-around;
}
.small-container{
max-width: 1080px;
margin: auto;
padding-left: 25px;
padding-right: 25px;
}
.col-4 {
flex-basis: 25%;
padding: 10px;
min-width: 200px;
margin-bottom: 50px;
transition: transform 0.5s;
}
.col-4 img {
width: 100%;
}
Thank you for your help!
I am currently seeking a JavaScript function that will allow me to successfully add products to the shopping cart without the use of any frameworks.