According to this answer and many others on SO i've seen ways to refresh Ajax content (after the success call) on the element if it has an ID. However, I need to get this behaviour on classes. I've tried variations of using $.each, foreach, this, etc but they all produce similar (incorrect) results. Can anyone teach me how to refresh the content for the current clicked item only?
This is how i'm doing it, but after clicking the report button, 16 more buttons appear, because it's calling all the buttons with that class.
<!--html:-->
<!-- If userReported function returns true, then user already reported this item. -->
<!-- This code block runs in a loop of about 10-17 iterations -->
<span class="report-btn_wrapper refresh-report-after-ajax">
<i <?php if (userReported($product_ref)) { ?> title="Report this item" class="fa fa-flag-o report-btn" <?php } else { ?> title="You reported this item" class="fa fa-flag report-btn" <?php } ?> data-id="<?=$product_ref;?>" data-uid="<?=$user_ref;?>"></i>
</span>
//javascript:
$('body').on('click', '.report-btn', function (e) {
var id = $(this).data('id');
var uid = $(this).data('uid');
$.ajax({
type: 'POST',
url: 'report.inc.php',
data: {
product_ref : id,
user_ref : uid
},
success: function (html) {
//give user a notification message
notification(html, 0);
//refresh the button (if user clicked, button is red, if user did not click, button is grey)
$(".refresh-report-after-ajax").load(window.location + " .refresh-report-after-ajax");
}
});
e.preventDefault();
});
What is happening:
What I'm trying to achieve: