3

Below is the prototype of what I am trying to do.

var entry_set   = $;

// start to loop data
for([])
{
    // create HTML element to represent that data
    $('<div></div>')
        .data([])
        .addClass([])
        .on([])
        .insertAfter(entry_set);
}

// modify DOM only once all the entries are consolidated to a single jQuery object
entry_set.appendTo('.entries');

The comments say it all. In short - the idea is to modify document DOM only once when inserting data. I would usually go HTML string approach (simply concatenating the same structure using a string), but I am interested whether anything similar to this might work as well.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • possible duplicate of [Merging two jQuery selections](http://stackoverflow.com/questions/2400797/merging-two-jquery-selections) – Felix Kling May 08 '12 at 12:45
  • 2
    Not a duplicate since I do not have an initial "selection", which is the main problem essentially. – Gajus May 08 '12 at 12:46
  • Then create an empty selection: `var entry_set = $()`. See also [Getting an empty JQuery object](http://stackoverflow.com/questions/897331/getting-an-empty-jquery-object) – Felix Kling May 08 '12 at 12:52
  • @FelixKling, you should have posted this as an answer, because that's the missing bit I was asking about. – Gajus May 08 '12 at 12:55

3 Answers3

2

You could create an empty DOM element and .append() to that

var entry_set = $("<div>"); //empty dom element

// start to loop data
var i = 4;
while(i--) {
    // create HTML element to represent that data
    var item = $('<div>', {
        text: "test " + i
    });
    entry_set.append(item);
}

// modify DOM only once all the entries are consolidated to a single jQuery object
$("body").append(entry_set.children());​

working demo at: http://jsfiddle.net/F2J6g/1/

EDIT You can also start with an empty collection and use .add()

var entry_set = $(); //empty collection

// start to loop data
var i = 4;
while(i--) {
    // create HTML element to represent that data
    var item = $('<div>', {
        text: "test " + i
    });
    entry_set = entry_set.add(item);
}

// modify DOM only once all the entries are consolidated to a single jQuery object
$("body").append(entry_set);

http://jsfiddle.net/F2J6g/2/

xec
  • 17,349
  • 3
  • 46
  • 54
0

@Guy, I don't think you will get the desired HTML output. try using "wrap". Sample Syntax:

$('.OuterDiv').wrap('<div class="abc" />');
avani gadhai
  • 460
  • 2
  • 12
0

Unfortunately, because those objects aren't actually attached to any heirarchy, calling insertAfter doesn't actually mean anything. What you'll need to do is put them inside a containing div, maybe something like this:

var entry_set = $('<div>');

// start to loop data
for([])
{
    // create HTML element to represent that data
    $('<div></div>')
        .data([])
        .addClass([])
        .on([])
        .appendTo(entry_set);
}

// modify DOM only once all the entries are consolidated to a single jQuery object
entry_set.appendTo('.entries');

I haven't tested that, but I think it should work.

LukeGT
  • 2,324
  • 1
  • 21
  • 20