0

I have a one div with id="div".

I need to replicate my div three times with new divs having:

id="div1"
id="div2"
id="div3"

I want to append div inside the body element as shown in this image.

enter image description here

$(document).ready(function () {
  for (i = 0; i < 3; i++) {
    $('body').append('#div1' + i + '');
  }
});
#div1{
  width: 167px;
  height: 30px;
  background-color: #ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
  div
</div>
jwpfox
  • 5,124
  • 11
  • 45
  • 42

6 Answers6

0

You could do something like this using the clone property of jquery

$(document).ready(function () {
  var a=$('body');
            for (i = 0; i < 3; i++) {
            a.append($("#div1").clone());
            
            }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<div id="div1">sdfs</div>
  </body>
  </html>
GraveyardQueen
  • 771
  • 1
  • 7
  • 17
0

You can do like this

var elem = document.getElementsByClassName('commonClass')[0].outerHTML
for(var i =0;i<3;i++){
$('body').append(elem)

}

DEMO

Note: This is just demo. Please adjust css accordingly

brk
  • 48,835
  • 10
  • 56
  • 78
0

Use clone(), attr() and appendTo() methods.

$(document).ready(function() {
  for (i = 2; i < 4; i++) {
    $('#div1')
      // clone the element
      .clone()
      // update id attribute of the element
      .attr('id', 'div' + i)
      // append to the body
      .appendTo('body')
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1" style="width: 167px;height: 30px; background-color: #ff0000;">div</div>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

var div = $("div[id^=div]:last");

for (var i = 1; i < 4; i++) {
  var newDivId = parseInt($(div).attr("id").match(/\d+/g), 10) + i;
  var newDiv = div.clone().attr('id', 'div' + newDivId);
  $('body').append(newDiv);
}
div[id^="div"] {
  width: 167px; height: 30px; background-color: #ff0000; margin:15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id='div1'>
  Lorum Ipsum
</div>

Credits: Roko's Answer

Community
  • 1
  • 1
philantrovert
  • 9,904
  • 3
  • 37
  • 61
0

Create div to append elements

<div id="divbox"></div>

CSS for div

.dclass {
  width: 100px;
  height: 20px;
  background:red;
  margin:10px;
}

Generate div and add html to other div

var container="";
for (i = 1; i <= 3; i++) {
  container += '<div id="div'+ i + '" class="dclass">'+ i + '</div>';
}
$('#divbox').html(container);

Demo

Komal
  • 2,716
  • 3
  • 24
  • 32
0

Try this one...

 $(document).ready(function() {
   var div = $('#div1');
   for (i = 2; i < 4; i++) {
     div = div.clone();
     div.attr('id', i);
     $('body').prepend(div);
   }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1" style="width: 167px;margin:15px;height: 30px; background-color: #ff0000;">div

</div>
Sankar
  • 6,908
  • 2
  • 30
  • 53