I've got a JavaScript function using a jQuery ajax $.post(... to query a database and then returns the JSON code back to be handled by a callback function.
<a href="#" iid="234" class="tooltp wish-clicked" data-original-title="Add to Watch List"><i class="icon-heart" style="color:red"></i> Add to Watch List</a>
Calling Function:
$(".wish-clicked").click(function(e){
e.preventDefault();
var callback = function (data, obj){
var obj = jQuery.parseJSON(data);
alert($(obj).html()); //B
if (!obj.error) {
$(obj).html("Watching");
} else {
alert("Failed to set item for watching");
}
};
alert($(this).html()); //A
setWishList($(this).attr("iid"), callback, this);
});
When the user clicks on the link with class .wish-clicked it fires this function.
setWishList() lives in another .js file and is:
function setWishList(iid, callback, obj) {
$.post("set_wish_list.php", {iid: iid}, function(data) {
phpErrPlusParam(data, callback, obj);
}).fail(function() { errLive(data) });
}
This passes the data through to function phpErrPlusParam defined as:
function phpErrPlusParam(data, callback, param) {
if (err(data)) {
alert('System Error: ' + data);
} else {
//C
return callback(data, param);
}
}
The callback then runs and handles the result. Problem is that I'm also passing in the 'this' object from the click event. Section marked //A will return the html of the element link being clicked. The section marked //B (which holds the same object (it still appears ok I've checked in chrome debugger) displays null.
I'm confused to what's happening here, somewhere along the line the object loses the ability to return html()??
EDIT: The point at which it stops giving the correct output is at //C (i.e. when it's given to the callback.
objinsetWishListandparaminphpErrPlusParamarenull?