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>
}
})
What is your AJAX code? Edit your message above and include it, please.
– Luiz Felipe
I edited the question!
– Thiago
<div class="rating1" id="rating"> Class exchanged with ID
– Pedro Augusto
Sorry, edited up!
– Thiago
After adding the new
div
in Document, you ran the code again$('.rating1').likeDislike(....
?– Ricardo Pontual
Try as follows: https://jsfiddle.net/u1e23r1o/
– Sam