jQuery .clone() method

0 votes
686 views
added Mar 29, 2018 in jQuery by Callum Sergeant (1,440 points)
edited Jul 12, 2018 by LC Marshal
//Clone e and insertafter it

$(document).ready(function() {
  var e = $('#project');
  for (var i = 0; i < 8; i++) {
    e.clone().insertAfter(e);
  }
});

 

8 Responses

0 votes
responded Mar 29, 2018 by Callum Sergeant (1,440 points)
edited Jul 12, 2018 by LC Marshal
var clone = $('#clone').clone();
var increment=0;
$('.addprop').live('click', function() {
    $(clone).clone().addClass('add-margin').attr("name","some-name"+increment).removeAttr('id').insertAfter(".addprop");
    increment++;
});
0 votes
responded Mar 29, 2018 by Callum Sergeant (1,440 points)
edited Jul 12, 2018 by LC Marshal

 

//clone the address  and append it as a new address
var newaddress= $("#addresses div.address").eq(0).clone();
newaddress.find('input').each(function() {
    this.name= this.name.replace('[0]', '['+i+']');
});
$('#addresses').append(newaddress);
0 votes
responded Mar 29, 2018 by Callum Sergeant (1,440 points)
edited Jul 12, 2018 by LC Marshal
//To clone specific selector
if ($("body").hasClass('page-project-outlook')) { 
   $('.page-header').html($("ol.breadcrumb li:last-child" ).html());  
}
0 votes
responded Apr 5, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//if the expiry message is exist on the body, clone the element to another element.

$(function() {
  var expiry = $('span.expiry_msg');  
  if ($(expiry).length) { 
   $(expiry).clone().appendTo('#get-expired-msg').addClass('expiry-on-header').fadeIn(500);
  }
});
0 votes
responded May 3, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//To clone button into .package class

$(function(){
  var $baby= $('.baby-element').clone();
  $('.mother').html($baby);
});

 

0 votes
responded May 3, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Clone element 

var catName = $('.forum-category > a'),
    crumbCat = $('ol.breadcrumb li:nth-child(4)'),
    catUrl = $(location).attr('href'),
    urlParts = catUrl.split("/"),
    endUrl = urlParts[urlParts.length-2],
    forumPage = '/property-forum/',
    resultUrl = forumPage + endUrl,
    CrumbCatLast = $(catName).attr('href', resultUrl).clone();
    // alert(endUrl);

$(catName).attr('href', resultUrl);
$(crumbCat).html(CrumbCatLast);

 

0 votes
responded Jun 29, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Clone #project to be 9 altogether
$(document).ready(function() {
  var e = $('#project');
  for (var i = 0; i < 8; i++) {
    e.clone().insertAfter(e);
  }
});
0 votes
responded Jul 26, 2019 by LC Marshal Captain (25,790 points)
// if element exist, do this
if(infEl.length) {
  $(mainContent).each(function () {
    this.append(nextData);
    sampleData.clone().insertAfter(sampleData);
  })
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...