jquery - Clone an element on document.ready, only stored in variable for first action -


i trying clone div , store in variable on page load, , append everytime button clicked. works when click button first time. idea why is?

$(document).ready(function() {    var inquiryitem;    inquiryitem = $('.firstitem').clone();      $('body').on('click', '.create-inquiry-add', function() {      $(inquiryitem).addclass('animated fadein newitem').removeclass('firstitem').insertafter('.inquiry-item:last-child');    });  });

you cloning once using append() method move same element (to same place guess). code should be:

$(document).ready(function () {     var $inquiryitem = $('.firstitem');     $('body').on('click', '.create-inquiry-add', function () {         $inquiryitem.clone().toggleclass('animated fadein newitem firstitem').insertafter('.inquiry-item:last-child');     }); }); 

using toggleclass() simplicity if former element hasn't animated fadein newitem classes set default.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -