Particularly referring to the jsFiddle in your comment above... You're using .data()
incorrectly. (Among other structural errors in the fiddle itself, such as not including jQuery, but I took care of that in my update below for you.)
In the question's code, I don't know what .listview
is even supposed to be. That term doesn't seem to be used anywhere else so I'm not sure what you were expecting form it. And in your jsFiddle code, you just have a jQuery selector ($("#videoList")
) which is just going to return a set of matched elements, not any kind of data attributes within those elements. The length of that set in this case is 1, because there's only one #videoList
element.
To set a data value, you would do something like this:
$("#videoList").data("foo", data);
Where "foo"
can be any key you want to associate with that value. Then you would use that same key to later get that value:
var list = $("#videoList").data("foo");
Updated jsFiddle.
Of course, what you're trying to do with this data is still a bit unclear. But effectively this is how you would use data-*
attributes with jQuery to store and fetch data within the DOM.
Note: The code in your question also implies a timing error whereby you are attempting to read a value before it has been set by an asynchronous operation. If this is indeed happening then you'll want to take a look at this question and its answers. Anything you do in response to an asynchronous operation would need to be invoked by a callback from that operation. Simply placing the code on the next line won't work.