Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What did I manage do do wrong here? I can't see it, through the first $.each it works, get to the second it stops..

var testJSON = {"cluster":[{"node":[{"name":"one", "number":'100', "error":"none"},{"name":"two", "number":'200', "error":"none"},{"name":"three", "number":'300', "error":"found"},{"name":"four", "number":'400', "error":"none"}]}]}

if (testJSON.cluster.length != 0)
{
    $.each(testJSON.cluster, function(i, clstr)
    {
        $('.clusters').append('<ul class="nodes">');
        $.each(clstr.node, function(i, ndes)
        {
            $.find('ul').append('<li>'+ndes.name+'</li>');
        });
        $('.clusters').append('</ul>');
    });
}
share|improve this question
1  
Stops? We'll need a bit more than that. Did you receive any uncaught exceptions? – Delan Azabani Aug 17 '11 at 15:11
does the outer each work in the first place? append() is adding a DOM node, not text - append ("</ul>") seems out of place. – BrokenGlass Aug 17 '11 at 15:15

2 Answers

up vote 7 down vote accepted

Change your code to the following:

if (testJSON.cluster.length != 0) {
    $.each(testJSON.cluster, function(i, clstr) {
        $('.clusters').append('<ul class="nodes"></ul>');
        $.each(clstr.node, function(i, ndes) {
            $('.clusters ul.nodes').append('<li>' + ndes.name + '</li>');
        });;
    });
}

When you append an element, you don't have to later append the closing tag. Also, find cannot be called directly from the jQuery object. You need a selector.

share|improve this answer

What is $.find, you are getting an exception in the inner loop and then it stops.

$.find('ul').append('<li>'+ndes.name+'</li>');

Are you looking for the ul which you added in the outer loop? If yes, then use

$('.clusters ul').append('<li>'+ndes.name+'</li>')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.