1

i m doing a ajax call with which i m getting data (html) which is having the structure

 <li>
<div class="min-height">
    <a href="auser.jsp?id=7"><img src="dir/image_759.jpeg"alt="image" /></a><br />
    sumit jha
</div>
</li>

Now before adding it the dom i want to count the number of li 's in the html

as for example

$.ajax( {
type: "POST",
url: "more_people.jsp",
data: ({last_id: last_id  }),
cache: false,
success: function(html)
{
    $li = html;
    console.log ( $li.html().children().length );
}
});

How do i do??

thanks

2 Answers 2

5

You need to .filter()help the result:

success: function(html) {
    console.log($(html).filter("li").length);
}
Sign up to request clarification or add additional context in comments.

3 Comments

answer right.. find is for ol > li level... not at the same level or at ol level..
@Pradyut: I don't understand. With the given HTML structure from your question, the <li> nodes are top leveled. In this case you need to invoke .filter() or to wrap the result around an extra-node to use .find().
some misunderstanding... your answer is right...no issues.. ;-)
0

You mean like this?

html = '<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>';
var temp = document.createElement('<div>');
temp.innerHTML = html;
alert(temp.getElementsByTagName('li').length);

2 Comments

jAndy answer is right... u r frm rus?? i like rus... and raw javascript...:D btw do ur answer without any temp using only the html...
:-) I'm English, surname is Polish, first name is better indication of nationality usually. Without the temp var? I don't think you can set the innerHTML then invoke getElementsByTagName without some sort of reference. Well, in IE you can do document.createElement(html).getElementsByTagName('li').length but I'm not sure that's cross-browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.