tags:

views:

214

answers:

3

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?

A: 

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...

Chris Thompson
http://www.w3.org/TR/REC-html40/struct/lists.html#edef-LI
+4  A: 

perhaps you want

$('div').append('<ul></ul>');
$.each(arr, function () {
    $('ul').append('<li>' + this);
});
FurtiveFelon
thank you! this totally works.
A: 

Try this:

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