0

I have a problem in jQuery, it is that I have an array to append to an object (id) This function also after intends such as.

When I now use a "each" function in jQuery do I do it this way.

$.each(childrenObj, function(index, value) 
{
    $( document.createElement('ol') )
    .append(
        $( document.createElement('li') )
        .attr('id', 'list_'+ listItem )
        .append(
            $( document.createElement('div') )
            .addClass('listContentItem')
            .html( value.contentTitle )
            )
        )
    .appendTo('#list_'+ ( listItem - 1 ) );

    listItem++;
});

As you would imagine, it will make a <ol><li>test</li></ol> where after doing the same thing over and over again depending on how many people run through.

That I would like is that I would like it to make a <ol> in an object after which I in my each loop adds <li> to it and eventually make a appendTo ('# id') after my desire but could not quite get it resolved and it causes some problems pure design front.

Hope there are some who sits with a solution and can help me a little on the road.

1 Answer 1

2

Create the ol outside your loop (below code is untested, but should hopefully work)

var ol = $( document.createElement('ol') );
$.each(childrenObj, function(index, value) 
{

    ol.append(
        $( document.createElement('li') )
        .append(
            $( document.createElement('div') )
            .addClass('listContentItem')
            .html( value.contentTitle )
            )
        );
});

ol.appendTo('#some_id');
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but my problem are its run into a loop, so i think my problem not is what i think but what you say its true, thanks from here.

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.