canacast
canacast

Reputation: 241

jQuery empty() callback not being fired

I'm emptying an element of all children, then applying an element with the same class but within separate element.

The callback on this empty() function isn't firing. Am I missing something with this class/callback issue?

$('.commentsButton').click(function(e){
e.preventDefault();
if ('.openComments').length) {
    $(this).closest('.box').removeClass(openComments);

    $('.commentsBox').empty(function(){
            $(this).closest('.box').find('.commentsBox').load('url.com');
            $(this).closest('.box').addClass(openComments);
        });
} else {
        $(this).closest('.box').find('.commentsBox').load('url.com');
        $(this).closest('.box').addClass(openComments);
    }
});

<div class="box">
  <a class="commentsButton" href="#"></a>
   <div class="commentsBox"></div>
</div>
<div class="box">
  <a class="commentsButton" href="#"></a>
   <div class="commentsBox"></div>
</div>

Upvotes: 0

Views: 2655

Answers (2)

adeneo
adeneo

Reputation: 318312

$('.commentsButton').on('click', function(e){
    e.preventDefault();

    $('.box').removeClass('openComments');
    $('.commentsBox').empty();
    $(this).closest('.box').addClass('openComments')
           .find('.commentsBox').load('url.com');
});

Upvotes: 0

Armen
Armen

Reputation: 1093

just call the empty function without any parameter and then do whatever you like.

$('.commentsBox').empty();
$('.commentsBox').each(function(){
      $(this).closest('.box').find('.commentsBox').load('url.com');
      $(this).closest('.box').addClass(openComments);
});

Upvotes: 1

Related Questions