using document.createDocumentFragment() child dom elements that contain jQuery.data

Posted by taber on Stack Overflow See other posts from Stack Overflow or by taber
Published on 2010-05-11T18:53:21Z Indexed on 2010/05/11 19:44 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

I want to use document.createDocumentFragment() to create an optimized collection of HTML elements that contain ".data" coming from jQuery (v 1.4.2), but I'm kind of stuck on how to get the data to surface from the HTML elements.

Here's my code:


var genres_html = document.createDocumentFragment();
$(xmlData).find('genres').each(function(i, node) {
    var genre = document.createElement('a');
    $(genre).addClass('button')
        .attr('href', 'javascript:void(0)')
        .html( $(node).find('genreName:first').text() )
        .data('genreData', { id: $(node).find('genreID:first').text() });
    genres_html.appendChild( genre.cloneNode(true) );
});

$('#list').html(genres_html);

// error: $('#list a:first').data('genreData') is null
alert($('#list a:first').data('genreData').id);

What am I doing wrong here? I suspect it's probably something with .cloneNode() not carrying over the data when the element is appended to the documentFragment. Sometimes there are tons of rows so I want to keep things pretty optimized, speed-wise.

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery