Im looking for a way, in jQuery, to check if a UL exists AND if it does count the number of LI elements are contained within it.
The following jQuery loads a UL into a hidden DIV if there are new records to be fetched from the database:
j(".refreshMe").everyTime(5000,function(i){
j.ajax({
url: "refresh.php?latest="+className+"",
cache: false,
success: function(html){
j(".refreshMe").html(html);
}
})
});
If a UL is returned I want to count how many LI elements are in the list, then write that value to the page.
How can I achieve this in jQuery?
You can do it like this:
In your
successhandler just use the returned data as the context to look in, so$("li", html)only looks for<li>elements in the server response, and.lengthis the count found, you can take the value and place it where you want it in the page.