-3

I am calling rest api through jQuery and i need to read the videoList value in JavaScript.

below my code,every time alert is saying undifined.

How to read videoList in javascript?

$(document).ready(function() {
  $.get('https://123/movies', function(data) {
    $("#videoList").data(data.entries);
  });
});

var list = $("#videoList").listview;
alert(list);
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
WPS
  • 21
  • 1
  • 6
  • 2
    what is `$("#videoList").listview;` supposed to do? – Kevin B Sep 19 '17 at 16:21
  • There's no property on the jQuery object called `listview`. So it's undefined. – David Sep 19 '17 at 16:22
  • 1
    and even if it did exist, it wouldn't contain a result that hasn't been received yet. – Kevin B Sep 19 '17 at 16:22
  • do u mean that you have added a custom data to a HTML element dynamically and need to know how to access it? If that is your question, add a custom data like $(element).data(dataName, data) and access as $(element).data(dataName). – Dhanasekar Murugesan Sep 19 '17 at 16:23
  • yes,i need to loop the data in html – WPS Sep 19 '17 at 16:27
  • @WPS: What do you even *mean* by that? What is the actual data you're getting from the server and what do you need to do with it? "loop it" doesn't tell us anything. – David Sep 19 '17 at 16:29
  • @David, I need to read $("#videoList").data(data.entries); this videoList in javascript.I mean var list how to do that – WPS Sep 19 '17 at 16:36
  • 1
    @WPS: You seem to be misunderstanding several things here, one of which being what `.data()` does in jQuery. If you want to *set* a data attribute, it would be: `.data('someKey', data.entries)` Then to later fetch this data, you'd get it from: `.data('someKey')` Of course, "someKey" can be any key that you want. How this will work with complex objects instead of simple values, I'm not certain. Additionally, the data will only be set *after* the asynchronous operation completes. Your code is trying to read it *before* it completes. – David Sep 19 '17 at 16:38
  • @WPS, Please update this question to include a sample of the html in `#videoList`, as well as what is passed as the `data` argument to the `$.get('https...')` callback function. Creating a simplified example in https://jsfiddle.net, and linking to that example, will go a long way toward people being able to help you. – Tony Chiboucas Sep 19 '17 at 16:39
  • here is the jsfiddle.net https://jsfiddle.net/58rax13d/5/ – WPS Sep 19 '17 at 16:49

2 Answers2

0

You are trying to alert the value before the ajax request is completed .Move

var list = $("#videoList").listview; alert(list);

To the body of callback function and it should work.

$(document).ready(function() { 
  $.get('https://123/movies', function(data) { 
    $("#videoList").data(data.entries);
    var list = $("#videoList"); 
    alert(list);
  }); 
}); 
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Able Johnson
  • 551
  • 7
  • 29
0

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.

David
  • 208,112
  • 36
  • 198
  • 279