views:

42

answers:

3

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>';
A: 

I think id is not getting generated what you are expecting, try to see what it turns out to be with:

alert("#more" + ID);

And make sure it is same as your code:

<a href="#" class="more" id="'.$dateTime.'">more</a>
Sarfraz
Why would the value of the `$dateTime` variable change from the first time he used it in his php `echo` snippet to the second time? That doesn't make sense (well, absent multi-threading, which I think is a safe bet).
T.J. Crowder
my id is going to be a date, i checked it, and a alert with `#more2010-09-20 12:53:43` is showing
getaway
@getaway: And that does not seem to be a valid id value as @T.J. Crowder pointed out.
Sarfraz
A: 

I think that

$("#more" + ID).remove();

should be

$("#more" + ID).html('');

or else things aren't going to work properly.

Pointy
+4  A: 

If your id has a space in it, that's your problem. The space is valid in an id attribute, but it messes up the CSS selector you're using here (because it's not escaped correctly):

$("#more" + ID).remove();

For instance, with the ID you quoted in a comment on another answer, that will end up being:

$("#more2010-09-20 12:53:43").remove();

...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 $dateTime variable'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_replace and look something like this (I'm not a PHP guy, apologies if there are minor syntax issues, but you get the gist):

$baseId = str_replace(':', '', str_replace(' ', '_', $dateTime));
echo'<div id="more'. $baseId.'" class="morebox"><a href="#" class="more" id="'.$baseId.'">more</a>';

Details on the CSS selectors thing: There are lots of valid id values that you can't use in CSS selectors unless you escape certain characters. Very nearly anything is valid in an id, but the ID you use with a CSS selector has to use a subset of that or be escaped correctly:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

T.J. Crowder
my $dateTime Variable is $dateTime=$row['dt']; which is retrieved from the database, which is a DATATIME value
getaway
@getaway: By the time `echo` is done with it, it's a string. I'd recommend converting it to a string explicitly, removing the colons, and replacing the space with an underscore, so you end up with something like `more2010-09-20_125343`. This is easily done with PHP's `str_replace` function.
T.J. Crowder
@tj your a the man! thank you
getaway
On a side note: `str_replace()` can use an array of characters, no need to nest them.
Wrikken
@Wrikken: Thanks. I was using different replacement chars, though (`:` with nothing, and space with `_`), though just using nothing for both would have worked.
T.J. Crowder
_both_ arguments can use an array ;) (but for clarities sake nesting them can be better).
Wrikken
@Wrikken: That's cool! Ah, yes, I see it in the documentation. Thanks, good to know! (See, I *said* I wasn't a PHP guy...)
T.J. Crowder