i have link:
<a class="tag" wi_id="3042" wl_id="3693" for_user_id="441" href="#a"> which triggers ajax call
$(".tag").click(function() { var for_user_id = $(this).attr("for_user_id"); var wl_id = $(this).attr("wl_id"); var wi_id = $(this).attr("wi_id"); $.ajax({ type: "post", url: "/ajax/actions/tag.php", data: { 'for_user_id': 'for_user_id', 'wl_id': 'wl_id', 'wi_id': 'wi_id' }, success: function(data){ $(this).text("you've tagged this"); $(this).closest('.gl_buttons_holder').toggleclass('gl_buttons_holder gl_buttons_holder_tagged'); $(this).closest('.gl_buttons').addclass('tagged'); } }); return false; }); but in console see following:
typeerror: e undefined the ajax file gets processed post data blank, , success actions not happen, gets posted zeros , classes not changed
i have stared , stared... obvious?
this not passed automatically ajax callback function. can use context: parameter tell jquery pass it:
$.ajax({ type: "post", url: "/ajax/actions/tag.php", data: { 'for_user_id': for_user_id, 'wl_id': wl_id, 'wi_id': wi_id }, context: this, success: function(data){ $(this).text("you've tagged this"); $(this).closest('.gl_buttons_holder').toggleclass('gl_buttons_holder gl_buttons_holder_tagged'); $(this).closest('.gl_buttons').addclass('tagged'); } });