0

I am doing an ajax post that returns some html and I want to find out how many divs with a class of entry there are in the returned html and then loop through them.

$.post(url, { 'data' : 'qdewde' }, function(data) {
    alert( $('entry', data).length );

    $('.entry', data).each(function() {
        // do something here
    });
}, 'html');

html returned:

<!DOCTYPE html>
<html lang="en">
<head>
<body>
    <div class="entry"><h1>Entry 1</h1></div>
    <div class="entry"><h1>Entry 2</h1></div>
    <div class="entry"><h1>Entry 3</h1></div>
</body>
</html>

The alert just outputs 0 though and I'm unable to loop through each .entry object. What am I doing wrong?

1 Answer 1

1

For this you can create a virtual div to hold the response then only you can get the length:

$.post(url, { 'data' : 'qdewde' }, function(data) {
    var vd = $('<div>', {html:data});
    alert(vd.find('.entry').length);
}, 'html');
Sign up to request clarification or add additional context in comments.

Comments

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.