简体   繁体   中英

How to Pass Object Value From A Javascript File to HTML File

I created an object with an array of products and I want to retrieve its value in a label in my html file but I don't know what's the right format. My javascript and html file are in separated files. Can someone help me with this. Thanks a lot!

 const database = [ { name: "test", brand: "test", description: "test desc", price: "100", }, { name: "test2", brand: "test brand", description: "test desc2", price: "200", }, { name: "test3", brand: "test brand", " description": "test desc3", price: "300", }, ]; function addToCart() { return console.log(database[0].name) }
 <div class="card"> <img src= "img/1.jpg" alt="Avatar"> <div class="container"> <span> <input type="text" placeholder="Enter items" id="productName" value=$database[0].name /> <button onclick="addToCart()" id="addToCart">Buy</button> </div> </div>

 const database = [ { name: "test", brand: "test", description: "test desc", price: "100", }, { name: "test2", brand: "test brand", description: "test desc2", price: "200", }, { name: "test3", brand: "test brand", " description": "test desc3", price: "300", }, ]; function addToCart() { document.getElementById('productName').value = database[0].name; }
 <div class="card"> <img src= "img/1.jpg" alt="Avatar"> <div class="container"> <span> <input type="text" placeholder="Enter items" id="productName"/> <button onclick="addToCart()" id="addToCart">Buy</button> </div> </div>

try this. it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM