Function javascript work with dynamically generated elements

Asked

Viewed 57 times

0

How do I make this function work with dynamically generated elements? It only works when and element is loaded along with page, but when Gero a div with ajax the function does not work!

$('.rating1').likeDislike({
        reverseMode: true,
        disabledClass: 'disable',
        click: function (value, l, d, event) {          
            var likes = $(this.element).find('.likes');
            var dislikes =  $(this.element).find('.dislikes');

            likes.text(parseInt(likes.text()) + l);
            dislikes.text(parseInt(dislikes.text()) + d);
        }     
    });

The ajax code:

$.ajax({
    url : link,
    type: 'GET',
    dataType: "html",
    preocessData: false,       
  })
  .done(function(msg2){  

     // Adiciono esse elemento dinamicamente
      <div class="rating rating1">
            <button class="btn btn-default like">Like</button>
            <span class="likes">0</span>
            <button class="btn btn-default dislike">Dislike</button>
            <span class="dislikes">0</span>
        </div>    
}

})
  • 1

    What is your AJAX code? Edit your message above and include it, please.

  • I edited the question!

  • <div class="rating1" id="rating"> Class exchanged with ID

  • Sorry, edited up!

  • After adding the new div in Document, you ran the code again $('.rating1').likeDislike(....?

  • Try as follows: https://jsfiddle.net/u1e23r1o/

Show 1 more comment

1 answer

1


When you bind to $('.rating1').likeDislike(.... it is applied only to elements that already exist in the DOM, you need to perform the same operation for those that are created dynamically after that. And I recommend that you raise them with a different class, novo, apply and then remove, so you avoid manipulating again those who have already had the associated functionality.

There are more robust ways to solve this problem, but follow an example:

$.ajax({
    url : link,
    type: 'GET',
    dataType: "html",
    preocessData: false,       
  })
  .done(function(msg2){  

     // Adiciono esse elemento dinamicamente
      <div class="rating rating1 novoRating">
            <button class="btn btn-default like">Like</button>
            <span class="likes">0</span>
            <button class="btn btn-default dislike">Dislike</button>
            <span class="dislikes">0</span>
        </div>  

    //aplica aos novos elementos
    $('.novoRating').likeDislike({
        reverseMode: true,
        disabledClass: 'disable',
        click: function (value, l, d, event) {          
            var likes = $(this.element).find('.likes');
            var dislikes =  $(this.element).find('.dislikes');

            likes.text(parseInt(likes.text()) + l);
            dislikes.text(parseInt(dislikes.text()) + d);       

        }     
    });

    //remove a classe 
    $('.novoRating').removeClass('novoRating');
}

});
  • It worked, but I didn’t need to put the class . new because only one button will appear at a time on the page. But generating the script on the return of ajax worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.