For my library project, I am trying to create a shopping cart feature. The issue I'm facing is that when I click on a specific book, I cannot add it to another localStorage.
This is my html <!--Knjige-->
<div class="container grid" id='knjige'></div>
This is my CSS:
.container{
margin: 50px; }
.grid{
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 20px;
align-items: start; }
.grid-card{
border: 1px solid #ccc;
box-shadow: 2px 2px 6px 0px rgba(0,0,0,0.3);
background: orange;
z-index: 9; }
.text:hover{
color: #fff;
cursor: pointer; }
.text{
padding: 0 20px 20px;
color: black;
font-weight: bold; }
And this is my JavaScript, please note that I'm not allowed to use jQuery
let knjige = [
{"Naziv":"4_50 From Paddington_ A Miss Marple Mystery",
"ID":"XA7JORPL",
"Autor":"Agatha Christie",
"Godina":"2007",
"Cena":546,
"Raspolozivo_stanje":50,
},
{"Naziv":"Lord Edgware Dies (1986, Berkley)",
"ID":"BPL6QUG5",
"Autor":"Agatha Christie",
"Godina":"1986",
"Cena":1041.06,
"Raspolozivo_stanje":15,
},
{"Naziv":"Murder at the Vicarage (2000, Signet)",
"ID":"T2CGKTQQ",
"Autor":"Agatha Christie",
"Godina":"2000",
"Cena":546,
"Raspolozivo_stanje":44,
},
{"Naziv":"Sparkling Cyanide (1989)",
"ID":"1QIFZZ4P",
"Autor":"Agatha Christie",
"Godina":"1989",
"Cena":1114.91,
"Raspolozivo_stanje":45,
},
{"Naziv":"The Mystery of the Blue Train",
"ID":"4C4XW7H2",
"Autor":"Agatha Christie",
"Godina":"1928",
"Cena":1041.06,
"Raspolozivo_stanje":"",
}
];
if(!localStorage.getItem('knjige')){
window.localStorage.setItem('knjige', JSON.stringify(knjige));
}
let knjigeLocalStorage = JSON.parse(window.localStorage.getItem('knjige'));
window.onload = function(show){
for(knjiga of knjigeLocalStorage){
show += `
<div class='grid-card'>
<div class='text'>
<h4>Title: ${knjiga.Naziv}</h4>
<p>Author: ${knjiga.Autor}</p>
<p>ID: ${knjiga.ID}</p>
<p>Year: ${knjiga.Godina}</p>
<p>Price: ${knjiga.Cena}</p>
<p>Available Quantity: ${knjiga.Raspolozivo_stanje}</p>
<button class='btn' id='dugme' onclick=''><i class="fas fa-shopping-cart"></i></button>
</div>
</div>
`;
};
document.getElementById('knjige').innerHTML = show;
};
I want to display the value of the specific book when the button is clicked. Hope I explained it clearly..
Thanks!