Himmators
Himmators

Reputation: 15016

How to wrap an array of elements in an element using jquery?

Using this code, I get an array of list-items:

var column = this.children().slice(0, columnLength).remove()

I wish to wrap them in a ul tag so I can later apply them like this:

this.html(filler);

I would expect this to work.

filler = filler.add(column.wrapAll('<ul />'))

Full code:

jQuery.fn.splitList = function(num) {
    var columnLength = Math.ceil(this.children().length/num), filler = $j();
    while(this.children().length > 0){
      var column = this.children().slice(0, columnLength).remove()
      filler = filler.add(column.wrapAll('<ul />'))
      console.log(filler);
    }
    this.html(filler);
};

Upvotes: 0

Views: 375

Answers (2)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93601

You just need to reference the newly wrapped-around parent UL in your add:

filler = filler.add(column.wrapAll('<ul />').parent());

http://jsfiddle.net/TrueBlueAussie/g5symcvy/

Upvotes: 0

Tristan Lee
Tristan Lee

Reputation: 1220

Would this work for your requirements:

filler = filler.add($("<ul></ul>").append(column));

I assume column is an Array of LI's.

Upvotes: 2

Related Questions