最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to clone an element and create it everytime a button is clicked - Stack Overflow

programmeradmin2浏览0评论

I want to copy the div element and paste it next to it when a button is clicked.

I want to copy the element with the id="box" and paste it next it everytime I click on the button.

<!DOCTYPE html>
<html>
<head>
<link href="/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>

<div style="width: 18rem;" class="card" id="box">
    <div class="card-body">
        <h5 class="card-title" id="Cat1"></h5>
        <p class="card-text">No Products Created</p>
        <a href="#" class="btn btn-primary">+</a>
    </div>
</div>

<button onclick="addItem()">Click Me!</button>
  
<script>
    function addItem() {
          //code goes here
    }
</script>
 
</body>
</html>

I want to copy the div element and paste it next to it when a button is clicked.

I want to copy the element with the id="box" and paste it next it everytime I click on the button.

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>

<div style="width: 18rem;" class="card" id="box">
    <div class="card-body">
        <h5 class="card-title" id="Cat1"></h5>
        <p class="card-text">No Products Created</p>
        <a href="#" class="btn btn-primary">+</a>
    </div>
</div>

<button onclick="addItem()">Click Me!</button>
  
<script>
    function addItem() {
          //code goes here
    }
</script>
 
</body>
</html>
Share Improve this question edited Jul 24, 2023 at 15:40 Kartikeya Chauhan asked Jul 23, 2023 at 20:20 Kartikeya ChauhanKartikeya Chauhan 357 bronze badges 5
  • 1 What, specifically, isn't working with your code? "which doesn't work" is not enough information for us to really help you debug this... Sidenote, running your code: let input = document.createElement('div'); followed by input.id("box"); triggers an error: "Uncaught TypeError: input.id is not a function" Please edit your question and provide more details. – Tim Lewis Commented Jul 23, 2023 at 20:25
  • @TimLewis Oh my bad I thought it was understandable enough. I want to copy the element with the id="box" and paste it next it everytime I click on the button. – Kartikeya Chauhan Commented Jul 23, 2023 at 20:28
  • 2 Yeah, sorry, your question is clear enough, but your attempt and what specifically is going wrong it not
发布评论

评论列表(0)

  1. 暂无评论