একটি ডিভের মধ্যে একটি অ্যারের উপাদানগুলিকে এম্বেড করার জন্য, আমাদের কেবল অ্যারের উপর পুনরাবৃত্তি করতে হবে এবং উপাদানটিকে ডিভিতে সংযুক্ত করতে হবে
এটি এভাবে করা যেতে পারে -
উদাহরণ
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
});
}; এই কোডটি অনুমান করে যে ডিভটিতে আমরা অ্যারের উপাদানগুলি প্রদর্শন করতে চাই একটি আইডি 'ফলাফল'।
এর জন্য সম্পূর্ণ কোড হবে −
উদাহরণ
<!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> আউটপুট

"ডেটা দেখান" বোতামে ক্লিক করলে, নিম্নলিখিতটি দৃশ্যমান হয় -
