2

I'm using the following script:

$.ajax({
    url: urls.notifications,
    type: 'POST',
    dataType: 'html',
    data: {timestamp: notification_timestamp},
    success: function(response) {
        var responseAmt = $(response).find("li").length;
        alert(responseAmt);
    }
});

This is the response I get:

<li class="notification" data-notification-id="117">

    <a href="http://local.dev/user/admin">
        <div class="avatar-container">
            <img src="http://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028">
        </div>
    </a>
    <div class="content">
        <p class="message">

                                                <a href="http://local.dev/user/admin"><b>admin</b></a> has commented on your post.                  
                <a href="http://local.dev/gag/image-example-10">
                    <b>Check it out</b>!
                </a>

        </p>
        <p class="timestamp">
            <i class="fa fa-clock-o fa-fw"></i>
            5 seconds ago           </p>
    </div>
</li>

Each response can contain 1 or many <li>'s like that, and I need to count them to update responseAmt variable, which holds the amount of notifications returned.

I tried:

$(response).find("li").size(); //shows 0
$(response).find("li").length; //shows 0

But this one works correctly:

$(response).find(".content").length; //shows correct amount

Anyone knows why I can't count <li>'s?

Aristona
  • 8,611
  • 9
  • 54
  • 80
  • Then Use `$(response).find(".notification").length;` insted of `$(response).find("li").length;` – joker Mar 03 '14 at 04:40
  • @joker: Why should that make a difference? If the element cannot be found via its tag name, why should be found via its class name? – Felix Kling Mar 03 '14 at 04:42
  • I did not read the html response from ajax ever but out of curiosity from your code; you are reading .content which is a class for div; is it possible to read
  • by it's class name? or do u have different classes for
  • .
  • – Akki619 Mar 03 '14 at 04:43
  • @FelixKling, In que he says that `$(response).find(".content").length;` //shows correct amount then he can also find a correct value from class – joker Mar 03 '14 at 04:45
  • Is the
  • first element in body. see this might be the reason http://stackoverflow.com/questions/3300332/jquery-find-on-data-from-ajax-call-is-returning-object-object-instead
  • – Akki619 Mar 03 '14 at 04:45
  • @joker: But `.notification` is the class of the `li` element itself. If an element cannot be found via its tag name, then its impossible to find it via its class name. – Felix Kling Mar 03 '14 at 04:47
  • @FelixKling yes what ever the tag name you can find from class name as altnative. – joker Mar 03 '14 at 04:49
  • @joker: I don't think you understand what I mean, maybe I'm not clear enough. See my answer for the reason why `$(response).find("li")` doesn't work. That's why `$(response).find(".notification")` wouldn't work either. – Felix Kling Mar 03 '14 at 04:51
  • @FelixKling i got it your are right, but i think if you can use a `$(response).find(".content").length;`, where .content is class of div. then whtevr LI is selector or not.it can be find from its class also. – joker Mar 03 '14 at 04:56
  • @joker: Yes, if every `li` element contains only one element with class `content`, that would work as well. – Felix Kling Mar 03 '14 at 05:06
  • @FelixKling ok. . .and your ans is right,so no need to work with class name. so leave it, and pleasure to conversation with u. – joker Mar 03 '14 at 05:13