As @C3roe mentioned, the error you're getting is apparently because you're using <script type="module" ...
for your script.js
. You were doing this because on script.js
, you imported addtoCart
function from another script (cart.js
).
script.js
import { addtocart } from "./cart"; // <-- THIS PART
addtocart(quantity, name, price, imgurl);
//for fun
var fun =0
//loading the html file first cus to get empty spans
document.addEventListener('DOMContentLoaded',()=>{
// ...
One quick fix that can be done is to just redeclare that addtoCart
function inside your script.js
so you don't have to import it.
script.js
// import { addtocart } from "./cart"; // <-- NO LONGER NEEDED
function addtocart(quantity, name, price, imgurl) {
const li = document.createElement('li');
li.textContent = `${name} - ${quantity} x ₹${price} - ${imgurl}`;
cart.appendChild(li);
}
addtocart(quantity, name, price, imgurl);
//for fun
var fun = 0
//loading the html file first cus to get empty spans
document.addEventListener('DOMContentLoaded', () => {
//getting all span files as ids
// ....
Now on your index.html
, you don't need to specify type='module'
for your script.js
.
index.html
<script type="module" src="script.js"></script> <!-- CHANGE THIS PART -->
<script src="script.js"></script> <!-- TO BE LIKE THIS -->
I've tested this on my local machine and it doesn't throw the error you specified but instead pops up an alert: