ऐरे के एलिमेंट्स को डिव में एम्बेड करने के लिए, हमें बस ऐरे पर पुनरावृति करने और एलिमेंट को डिव में जोड़ते रहने की आवश्यकता है।
इसे इस तरह किया जा सकता है:
उदाहरण
const myArray = ["stone","paper","scissors"]; const embedElements = () => { myArray.forEach(element => { document.getElementById('result').innerHTML += `<div>${element}</div><br />`; // here result is the id of the div present in the DOM }); };
यह कोड यह मान लेता है कि जिस डिव में हम ऐरे के एलिमेंट्स प्रदर्शित करना चाहते हैं उसकी आईडी 'result' है।
इसके लिए पूरा कोड होगा:
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="result"></div> <button onclick="embedElements()">Show Data</button> <script> { const myArray = ["stone","paper","scissors"]; function embedElements(){ myArray.forEach(el => { document.getElementById('result').innerHTML +=`<div>${el}</div><br />`; // here result is the id of the div present in the dom }); }; } </script> </body> </html>
आउटपुट

"Show Data" बटन पर क्लिक करने पर, निम्नलिखित दिखाई देता है:
