I have a jQuery plugin that creates a list of news items and appends the <ul> to the element(s) calling the plugin. Each <li> gets a document key added to its data.
A brief snippet of the code:
- (function ($) {
- $.fn.NewsList = function (options) {
- var defaults = { ticker: '', onComplete: function () { } }
- var options = $.extend(defaults, options);
- var elements = $(this);
- elements.showLoading();
- var newsWrapper = $('<ul/>').addClass('list-content headlineNewsList');
- $.ajax({
- // AJAX call stuff removed for brevity
- success: function (result) {
- var msg = result.d;
- $.each(msg, function (index, value) {
- var newsListItem = $('<li/>').addClass('list-item-news');
- // Misc. variable assignments removed for brevity
- newsListItem.html(// HTML removed for brevity);
- newsListItem.appendTo(newsWrapper);
- newsListItem.data({ docKey: newsDocKey }) // newsDocKey is a string assigned above
- });
- },
- error: function (request, status, throwerror) {
- var newsListItem = $('<li/>').addClass('list-item-news');
- newsListItem.html(// HTML removed for brevity).appendTo(newsWrapper);
- },
- complete: function () {
- elements.append(newsWrapper).fadeIn();
- options.onComplete.call(this);
- return this;
- }
- });
- };
- } (jQuery));
On one page, I call the plugin via the following:
Works great, I see the data in Firebug, and everything is peachy.
- $('#Headlines').NewsList();
On a different page, however, when using the following code, the data does not exist:
- $('.headlineList').NewsList({ ticker: currentCompany.Ticker,
- onComplete: function () {
- $('.headlineNewsList').children().each(function () {
- alert($.data($(this),'docKey')); // This returns null
- });
- }
- });
None of the <li>'s have the data attached to them. I've tested the selector shown above in line 3, and it does pick up the correct <li>'s. The data is not visible in Firebug as it is on the other page. Both functions are called after document.ready and insert the elements after page load.
The only discernible differences between these two circumstances is that in the working case, the plugin is called against an ID, whereas in the nonworking instance, it is called against a CLASS. In addition, in the working case, only one DIV is affected, and in the nonworking case, there are two DIVs that receive the treatment.
Any thoughts out there from any jQuery geniuses? Thanks for any help you send my way... been banging my head on a table for hours with this.