Source Code:
(back to article)
Submit
Result:
Report an issue
<head> <style> .card { border: 1px solid #ccc; border-radius: 5px; padding: 10px; margin: 10px; width: 200px; } .card h3 { margin: 0; } </style> </head> <body> <div id="template-container"> <!-- Template element --> <template id="card-template"> <div class="card"> <h3 id="card-title">Title</h3> <p id="card-content">Content goes here...</p> </div> </template> </div> <div id="card-container"> <!-- Cards will be inserted here --> </div> <script> // Data for multiple cards const cardData = [ { title: 'Card 1', content: 'This is the first card.' }, { title: 'Card 2', content: 'This is the second card.' }, { title: 'Card 3', content: 'This is the third card.' } ]; // Function to create and insert cards function createCards(data) { const template = document.getElementById('card-template'); data.forEach(item => { const clone = document.importNode(template.content, true); // Customize the cloned content clone.querySelector('#card-title').textContent = item.title; clone.querySelector('#card-content').textContent = item.content; // Insert the cloned content into the DOM document.getElementById('card-container').appendChild(clone); }); } // Create cards with the provided data createCards(cardData); </script> </body>