views:

196

answers:

6

I have a ajax code which returns the list items as

<li>one</li>
<li>Two</li>

Each time it will return different number of <li>'s. I want to check the number of <li> it returns. How to check it using Javascript. Please Help.

+2  A: 

ajaxHtml.split('<li').length - 1 for counting the raw ajax html.

Or, element.getElementsByTagName("li").length (with element being a parent container for the LI tags.)

jojaba
I have <li> items in normal html page too. I need only the no if <li>'s ajax page returning
Rajasekar
He wants to check the # of li tags returned from an AJAX request -- probably before the elements are added to the DOM. Your answer counts the total `li` tags on the entire page.
Erik
+1  A: 

Have you tried:

var elements = document.getElementsByTagName('li');
var count = elements.length;
Darmen
A: 

FIXED: Some people thought that I am counting LIs on link click event and also not counting it from html response. Here is the edited answer.

If you got html response something like this:

myhtml.html:

<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>

Then your jQuery part may be look like this:

$.get('myhtml.html', function(data) {
   alert( $(data).find("li").length ); // 3
});
NAVEED
This is so unnecessarily complicated that it's not even funny.
amphetamachine
That PHP looks like HTML to me.
Russell Dias
@amphetamachine: But it is working and I code it to check it quickly. But your comment is so funny.
NAVEED
Russell Dias: I think you did not write a php file with html tags.
NAVEED
It is strange you are using jQuery but not really using it, `$("#ULs li").length` would do well. Either way, this ignores the AJAX part.
Kobi
@Kobi: Thanks for enhancement. I used jQuery to capture the link click event.
NAVEED
@Naveed: No I did not write a php file with html tags.
Russell Dias
@Russell Dias: But I wrote and it worked.
NAVEED
I never said it didn't work mate =) I was merely joking about the PHP being HTML.
Russell Dias
I was joking too.
NAVEED
A: 

If you need to look within a specific list, jQuery makes it simple:

$("DIV_NAME > ul > li").length

ChrisCast
A: 

If the list you want to test is coming in as a string from an AJAX request, you'll just treat it like a string, instead of a set of DOM objects.

Take a look at this bit of code.

var input = "<li>one</li><li>Two</li>";
var pattern = /<li>/gi;

var match;
var count = 0;
while(match = pattern.exec(input)) {
    count++;
}

alert("There are "+count+" occurences of <li> in the input string.");

What I'm doing here is using an regular expression to find occurrences of <li> case insensitively (just to be certain) and globally (treat the entire string as a single block of text to be searched).

Joel Verhagen
+4  A: 

Here ya go:

$(returnedHTML).find('li').length

This takes the returnedHTML and only counts the line items within it.

Erik
@sparr it absolutely will not. [From the manual](http://api.jquery.com/find/): `the method accepts a selector expression of the same type that we can pass to the $() function. The elements will be filtered by testing whether they match this selector.`
Erik
deleted my comment, was thinking of a different function
Sparr