I have a type of newsfeed wall like Facebook were you can create posts. I want when I click on the glyphicon in my post to remove that certain post.
The code is:
function loadPosts(){
$('#allPosts').empty();
for (var i = 0; i < aPosts.length; i++){
var postData = aPosts[i];
var comments ="";
for (var j = 0; j < postData.comments.length; j++ )
{
comments += '<p class="myComment">' + postData.comments[j].from + '<strong>says: </strong>'+ postData.comments[j].text +'</p>';
}
$('#allPosts').prepend('<div data-postId="'+i+'"class="wrapPost"><span class="glyphicon glyphicon-remove"></span>' +
postData.from +' '+'' +
'<strong>wrote :</strong>'+
'<li>' + postData.text + '</li>' + comments +
'<input type=text class=commentBox placeholder=Comment..>' +
'<button data-postId="'+i+'" class="postComment">Post</button></div>');
$('.glyphicon-remove').click(function() {
var postId = $(this).parent().attr('data-postId');
console.log(postId);
aPosts.splice(i, 1);
});
}
}
When I console log "postId" it tells me that I am at least clicking the right post, but it wont delete it from localStorage.
aPosts.splice(i, 1);
toaPosts.splice(parseInt(postId), 1);
does that help? I would also take the click event definition out of the for loop. – mccannf Oct 2 at 8:02.remove()
the icon's parent that was clicked. – DOC ASAREL Oct 2 at 8:05