I am beginner with javascript and I have following problem. The output data when i am invoking alert in my script is [Object object]. Following function is invoked when the button is clicked(onClick
).
There are [Object object]
elements in the array.
And the last line of of code doesn't work properly, I mean, innerHtml
.
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="center">
<h1 align="center">Shop Basket</h2>
<script type="text/javascript" src="external.js"></script>
<table align="center">
<tr>
<th align="left">Price</th>
<th>Product</th>
<th></th>
</tr>
<script>
let products = [{
20: "dysk ssd"
},
{
1100: "pralka"
},
{
219: "pad"
},
{
500: "monitor"
},
{
789: "i5 processor"
},
{
88: "soundblaster"
},
{
220: "mysz logitech"
},
{
219: "klawiatura modecom"
},
{
900: "gtx 1060"
},
{
823: "rx 570"
}
];
let shopBasket = [];
function addToBasket(value) {
shopBasket.push(value);
alert(shopBasket);
document.getElementById("change").innerHtml = "" + shopBasket.length;
}
for (let i = 0; i < products.length; i++) {
for (let key in products[i]) {
document.write("<tr>");
document.write("<td>" + key + "</td>");
document.write("<td>" + products[i][key] + "</td>");
document.write('<td><input value="Add to ShopBakset" type="button" onClick="addToBasket(\'' + products[i] + '\')"/></td>');
document.write("</tr>");
}
}
</script>
</table>
<center>
<a href="html-link.htm"><img src="shopbasket.jpg" title="basket" alt="basket"></a>
</center>
</div>
<p id="change"></p>
</body>
</html>
Regards