So I’m not really good in JavaScript, but here’s something I’m kind of stuck with:
So, I got this:
$(".post-click").click(function()
{
var classid = $(this).attr('id');
var postid = $(this).attr('id');
postid = postid.replace('post-click-id_', '');
alert("ID: " + classid + " PostID: " + postid);
$(this).replaceWith('<img SRC="assets/img/refresh.gif" ALT="" >');
$.post("likepost.php", { postid: postid } , function(data)
{
if(data.indexOf("Yes") >= 0)
{
//Question is about this part
}
else
{
//Question is about this part
}
});
});
Now, at that “Yes” or else part: How can I do it so I can replace the data from $(this) with replaceWith? I thought I could do it with classid, but I’m not really sure how to do that. I thought about this:
$(classid).replaceWith('Yes, yes indeed.');
$(classid).replaceWith('Nope.');
How would I make this work?
Assuming I’ve understood the question correctly and you’re attempting to replace the clicked element inside the
$.postcallback, the easiest thing to do will be to maintain a reference to that element outside of the callback. This saves you from having to traverse the DOM again to reselect an element you have already selected once:Your current attempt doesn’t work because
classidis simply a string representing the value of theidattribute. To create a jQuery object from it, you would need to append it to a “#” to produce a valid selector.