0

On click, a div is appended, on second click another div is created with the same contents making it an exact duplicate.

Q: How do i destroy the previous div when a new one is created?

$(document).on('click', '.LibSectOpen', function() {
         var LibSect = ($(this).find('.SectionName').html())
         $(this).find('.SectionName').empty()
         $(this).append('<div class="LibraryBooks BooksHolder"><img height="30" width="30" class="LibraryBooksGIF" src="../images/ic_loading.gif"></div>').load(function(e) {

        });
user3109875
  • 828
  • 12
  • 35
  • possible duplicate of [After first click, If ajax is done, on second click e.stopPropagation](http://stackoverflow.com/questions/21722694/after-first-click-if-ajax-is-done-on-second-click-e-stoppropagation) – Arun P Johny Feb 12 '14 at 08:48
  • 1
    you just asked the same question in another thread - http://stackoverflow.com/questions/21722694/after-first-click-if-ajax-is-done-on-second-click-e-stoppropagation/21722742#21722742 – Arun P Johny Feb 12 '14 at 08:49

2 Answers2

1

Try this.

HTML

<div id="container"></div>
<button>Add Content</button>

jQuery Code

$('button').on('click', function(){
        $('#container').empty();
        $('#container').append('<div class="LibraryBooks BooksHolder">this is dynamicaly added text</div>');

});

Demo

Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33
1

You should remove the old div at first before appending the new one.

$(document).on('click', '.LibSectOpen', function() {
     var LibSect = ($(this).find('.SectionName').html());
     $(this).find('.SectionName').empty();
     $(this).find('.BooksHolder').remove();
     $(this).append('<div class="LibraryBooks BooksHolder"><img height="30" width="30" class="LibraryBooksGIF" src="../images/ic_loading.gif"></div>');
});
jansesun
  • 108
  • 5