jQuery(document).ready(function ($) { // Some event will trigger the ajax call, you can push whatever data to the server, // simply passing it to the "data" object in ajax call console.log(ajax_object.ajaxurl); $("[data_ml_action]").on("click", function () { console.log("clicked"); var actionButton = $(this); actionButton.prop("disabled", true); actionButton.find("i").hide(); actionButton.find(".ml-loading-dots").show(); // actionButton.hide(); var postID = actionButton.attr("data_ml_post_id"); var actionType = actionButton.attr("data_ml_action"); var addOrRemove = actionButton.attr("data_ml_add_or_remove"); var userID = actionButton.attr("data_ml_user_id"); var labelReplacementAdd = actionButton.attr("data_ml_ajax_label_add"); var labelReplacementRemove = actionButton.attr("data_ml_ajax_label_remove"); $.ajax({ url: ajax_object.ajaxurl, // this is the object instantiated in wp_localize_script function type: "POST", data: { action: "action_submit", // this is the function in your functions.php that will be triggered action_type: actionType, add_or_remove: addOrRemove, post_id: postID, user_id: userID, }, success: function (data) { console.log(actionButton.find("span")); if (addOrRemove == "add") { if (actionType == "complete") $("[data-ml-ajax-id='" + postID + "']").removeClass( "ml-check-hide" ); actionButton.find("span.label").text(labelReplacementRemove); actionButton.find(".icon_add").hide(); actionButton.find(".icon_remove").show(); actionButton.attr("data_ml_add_or_remove", "remove"); } else if (addOrRemove == "remove") { if (actionType == "complete") $("[data-ml-ajax-id='" + postID + "']").addClass("ml-check-hide"); actionButton.find("span.label").text(labelReplacementAdd); actionButton.attr("data_ml_add_or_remove", "add"); actionButton.find(".icon_add").show(); actionButton.find(".icon_remove").hide(); } actionButton.find("i").show(); actionButton.find(".ml-loading-dots").hide(); actionButton.prop("disabled", false); }, }); }); });