I’m having trouble changing text in an element that is near another element. The function is fairly straightforward, but for some reason I can’t get this text change to work.
When I click on #dislike_text, it goes blank (as it should), but the .like_text doesn’t change. Note that there is also a postlike function (identical to this one, but reversed, which works fine).
function postdislike(elm) {
var comment_id = $(elm).attr('href');
$('#stuff').load(// --function--
function (data) {
$('#dislike_text' + comment_id).html(""); //works fine
$(elm).closest('.like_text').html('Like'); //doesn't work
});
return false;
}
closestgoes up through the ancestor elements and finds the first that matches the selector. If there are no ancestor elements with the classlike_text, no elements will be selected.I presume you do not mean to blank an ancestor element, since that would have the effect of removing the
#stuffelement. You therefore need to find your way to the other element in some other way, using jQuery’s traversing functions. The most likely way to do this is to useclosestto find a common ancestor, and thenfindto get the element itself. For instance: