Skip to content Skip to sidebar Skip to footer

How To Repeat Div Using JQuery Or JavaScript?

I have an HTML page with the following div tags

Solution 1:

So you want cloneNode of your div, so try something like this

var div = document.getElementById('myDiv'),
clone = div.cloneNode(true);
clone.id = "some_id";
document.body.appendChild(clone);

Solution 2:

Something like this? Sample Fiddle

var lastSection = $('[data-role="page"]:last'); //get the last page probably you can use this to increment the id based on last id
var newSection = lastSection.clone(false);//default is false anyways, if you want to carry over data and events you can use true.
newSection.attr('id','setthenewid');//set the new id
$('div[class^=dydivarr]',newSection).attr('class','newclass'); //set the new class for your divarr
$('.ui-btn-right a',newSection).attr('href','newurlmap');//set the new url map 
lastSection.after(newSection); //place the entire page section in the end

Solution 3:

Update :

Simple Code worked. It was just my wrong approach

<script type="text/javascript">

for(var lp=0; lp < 10; lp++)
{
document.write('<div data-role="page" id="page'+lp+'" data-theme="a">');
document.write('  <div data-role="content">');
document.write('    <center>');
document.write('      <h3><a href="#page0" data-role="none"><img src="images/logo.png" /></a></h3>');
document.write('     </center>');
document.write('     <div class="dydivarr+lp+'"> </div>');
document.write('     <br /> ');
document.write('     <div data-role="controlgroup" class="ui-btn-left"> <a href="#page'+(lp-1)+'"     data-role="button">Previous</a> </div>');
document.write('     <div data-role="controlgroup" class="ui-btn-right"> <a href="#page'+(lp+1)+'" data-role="button">Next</a> </div>');
document.write('   </div>');
document.write(' </div>');

}

</script>

Thanks


Solution 4:

How can I replicate a div a certain number of times based on a select-box value?

Roko C. Buljan

And

Vivin Paliath

's Answers


Post a Comment for "How To Repeat Div Using JQuery Or JavaScript?"