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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -