Nerdkowski
Nerdkowski

Reputation: 475

jQuery wrap each array items

I try to wrap each array item inside a <li></li> and save into a variable.
After that insert a <ul> at the beginning and a </ul> at the end.

What I've got so far:

$("document").ready(function() {
  var array = ["one", "two", "three", "four", "five"];

  jQuery.each(array, function(index, value) {
    value[index].wrap("<li></li>");
    var listItems = value[index].join("\n");
  });
});

But I get this error:

value[index].wrap is not a function

Upvotes: 1

Views: 1008

Answers (1)

Thomas Peri
Thomas Peri

Reputation: 103

Updated your fiddle: https://jsfiddle.net/vkq3hnzu/2/

var array = ["one", "two", "three", "four", "five"],
  ul = $('<ul>');
jQuery.each(array, function(index, value) {
  $('<li>').text(value).appendTo(ul);
});
ul.appendTo('body');

Upvotes: 2

Related Questions