i have a lot of jQuery Event listeners that initialize when the document finishes loading. that part is working fine. I have a function that makes ajax calls and gets responses as strings of HTML that I append with jQuery to parts of the DOM. Although the append works, THAT new HTML snippet of code doesn't work with the jQuery event listeners because it wasn't part of the DOM when it loaded. Is there a way to reinitialize jQuery event listeners?
Here is another thing: I tried to store the event listener(s) in a function (function a
) and call that function whenever I wanted the listeners reinitialized. That worked partially... Although the listeners responded to to new HTML snippets, the results of the listeners were doubled, and keeps doubling whenever function a()
is called, resulting in a major bug.
I'm Mixing jQuery and Angular. Here is a snippet:
$(document).ready(function(){
$('.like-btn').click(function(){
var likeStatus = $(this).data('like-status-json');
var contentType = $(this).data('content-type');
var contentID = $(this).data('content-id');
var likeMeter_id = '#' + contentType.toLowerCase() + '-' + 'likemeter' + '-' + contentID;
var likeMeter_elm = $(likeMeter_id);
// console.log( likeStatus, contentType, $(likeMeter_elm) );
var obj = {
likeStatus: likeStatus,
contentType: contentType,
contentID: contentID,
likeMeter_elm: likeMeter_elm,
likes: parseInt( $(likeMeter_elm).text() ),
og_elm: $(this)
}
$scope.likeAction(obj);
});
});
$scope.likeAction = function(dataObj) {
// console.log(dataObj);
var req = {
method: 'POST',
url: '/action/ajax/',
headers: {
'Content-Type': 'application/json',
'responseType': 'json',
"Accept" : "application/json",
'X-CSRFToken': Cookies.get('csrftoken')
},
data: {
action: dataObj.likeStatus.action,
info: dataObj,
csrfmiddlewaretoken: Cookies.get('csrftoken'),
}
}
$http(req).then(function(resp){
// Success Callback
// console.log(resp);
$(dataObj.og_elm).data('like-status-json', resp.data.likeStatus);
$(dataObj.og_elm).removeClass(dataObj.likeStatus.class).addClass(resp.data.likeStatus.class);
$(dataObj.og_elm).children('span.like-text').text(resp.data.likeStatus.text);
$(dataObj.likeMeter_elm).text(resp.data.likeMeter);
},
function(resp){
// Error Callback
// console.log(resp);
});
}
Here is the ajax that appends new HTML snippets:
$scope.addPostCommentUser = function(inputELM, dataObj) {
if( inputELM == undefined || dataObj == undefined ) {
console.log('Missing Inputs...');
return;
}
console.log(dataObj);
// return;
var req = {
method: 'POST',
url: '/action/ajax/',
headers: {
'Content-Type': 'application/json',
'responseType': 'json',
"Accept" : "application/json",
'X-CSRFToken': Cookies.get('csrftoken')
},
data: {
action: 'addPostCommentUser',
info: dataObj,
csrfmiddlewaretoken: Cookies.get('csrftoken'),
}
}
$http(req).then(function(resp){
// Success Callback
console.log(resp);
var id = '#cmlst-' + dataObj.post_id;
$(id).append(resp.data.comment_html)
$(inputELM).val('');
},
function(resp){
// Error Callback
console.log(resp);
});
}