I’m trying to remove this element on success function, but it’s not doing it!!
jQuery file:
$(function () {
//More Button
$('.more').live("click", function () {
var ID = $(this).attr("id");
if (ID) {
$("#more" + ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg=" + ID,
cache: false,
success: function (html) {
$("ul.statuses").append(html);
// my problem is here
$("#more" + ID).remove();
}
});
}
else {
$(".morebox").html('The End');
}
return false;
});
});
This is the html file:
This is the button that’s clicked to retrieve more results!
echo'<div id="more'. $dateTime.'" class="morebox">
<a href="#" class="more" id="'.$dateTime.'">more</a>';
If your
idhas a space in it, that’s your problem. The space is valid in anidattribute, but it messes up the CSS selector you’re using here (because it’s not escaped correctly):For instance, with the ID you quoted in a comment on another answer, that will end up being:
…which is an invalid selector (I’m pretty sure; at the very least, it isn’t the selector you’re looking for).
The easiest thing is probably to ensure that your
$dateTimevariable’s value doesn’t have any spaces in it. For good measure, I’d avoid the colons (:) as well, but I’m probably just being paranoid(no, I’m not, see below).So for instance, you can change your PHP code that outputs these to use
str_replaceand look something like this (I’m not a PHP guy, apologies if there are minor syntax issues, but you get the gist):Details on the CSS selectors thing: There are lots of valid
idvalues that you can’t use in CSS selectors unless you escape certain characters. Very nearly anything is valid in anid, but the ID you use with a CSS selector has to use a subset of that or be escaped correctly: