jQuery $.data(): Possible misuse?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-05-04T02:11:10Z Indexed on 2010/05/04 2:18 UTC
Read the original article Hit count: 225

Filed under:
|

Perhaps I'm using $.data incorrectly.

Assigning the data:

var course_li = sprintf('<li class="draggable course">%s</li>', course["fields"]["name"]);

$(course_li).data('pk', course['pk']);
alert(course['pk']); // shows a correct value

Moving the li to a different ul:

function moveToTerm(item, term) {
    item.fadeOut(function() {
        item.appendTo(term).fadeIn();
    });
}

Trying to access the data later:

$.each($(term).children(".course"), function(index, course) {
      var pk = $(course).data('pk');
      // pk is undefined
      courses.push(pk);
});

What am I doing wrong? I have confirmed that the course li on which I am setting the data is the same as the one on which I am looking for it. (Unless I'm messing that up by calling appendTo() on it?)

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript