0

i have an array:

arr = ['a', 'b', 'c', ... ]

i want it to be:

 <div>
  <ul>
    <li>a
    <li>b
    <li>c
    ...
  </ul>
 </div>

so i'm trying:

$('div').append('<ul>');
$.each(arr, function () {
    $('div').append('<li>' + this);
});
$('div').append('</ul>');

but doesn't seem working... how can i queue this?

3 Answers 3

5

perhaps you want

$('div').append('<ul></ul>');
$.each(arr, function () {
    $('ul').append('<li>' + this);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Well, part of the problem is that you never close the li tags so try

$('div').append('<li>' + this + '</li>');

For starters...

Something else you can try is the function argument can have the signature

function(data, i)

Where data is the current element and i is the count. You might have more success accessing each element that way. Although, if memory serves, "this" should work in those instances...

Comments

0

Try this:

$('div').append('<ul/>);
$.each(arr, function () {
    $('div ul').append('<li>' + this + '</li>');
});

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.