Reputation: 5566
I have a simple sidebar which I want to display multiple videos from json, for now, I want just to show title and video from json something like this below.
Here is what I have so far.
<ul class="sidebar">
<li>
<h1 class="title"></h1>
<video id="video_list" src=""></video>
</li>
</ul>
Here is javascript
$(function() {
var movies = [{
"title": "travel",
"left": 201,
"top": 209,
"movieid": "10",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 81,
"top": 51,
"start_time": 1,
"end_time": 2,
"buttonid": "10_1",
"btn_url": "http://techslides.com/demos/sample-videos/small.mp4"
}]
},
{
"title": "ecommerce",
"movieid": "20",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 0,
"top": 0,
"start_time": 1,
"end_time": 2,
"width": '200',
"height": '60',
"buttonid": "20_1",
}]
},
];
function getVideoList() {
var title = $('.title');
var videoID = $('.video_list');
$.each(movies, function(index, dataValue) {
var movie_title = $("h1", {
value: dataValue.movieid
}).html(dataValue.title).appendTo(title);
var movie_video = $("#video_list", {
}).html(dataValue.movie_url).appendTo(videoID);
});
}
getVideoList();
});
Here is jsfiffle: live demo I am able to display video titles for each movie but I am struggling to display both title and corresponding video as in json :(
What am I supposed to change to get what I want???
Upvotes: 0
Views: 112
Reputation: 245
You have to create dynamically new element for each item in the list.
Javascript:
var movies = [{
"title": "travel",
"url": "http://techslides.com/demos/sample-videos/small.mp4"
}, {
"title": "ecommerce",
"url": "http://techslides.com/demos/sample-videos/small.mp4"
}];
var sidebar = $('.sidebar');
movies.forEach(function(movie) {
sidebar.append('<li><h1>' + movie.title + '</h1><video src="' + movie.url + '"></video></li>');
});
HTML
<ul class="sidebar"></ul>
Working demo: https://jsfiddle.net/omg0zkct/3/
Upvotes: 1
Reputation: 30893
Your loop is not defining each element. If you want a <li>
for each title and video, you need to do something different. Consider making a function that builds the template for each container.
Working Example: https://jsfiddle.net/Twisty/e2q4kd13/12/
HTML
<ul class="sidebar">
</ul>
JavaScript
$(function() {
var movies = [{
"title": "travel",
"left": 201,
"top": 209,
"movieid": "10",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 81,
"top": 51,
"start_time": 1,
"end_time": 2,
"buttonid": "10_1",
"btn_url": "http://techslides.com/demos/sample-videos/small.mp4"
}]
},
{
"title": "ecommerce",
"movieid": "20",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 0,
"top": 0,
"start_time": 1,
"end_time": 2,
"width": '200',
"height": '60',
"buttonid": "20_1",
}]
}
];
function formatTitle(t) {
var nt = t[0].toUpperCase();
nt += t.slice(1);
return nt;
}
function makeListItem(v, p) {
var li = $("<li>");
var title = $("<h1>", {
class: "title",
for: "video_" + v.movieid
}).html(formatTitle(v.title)).appendTo(li);
var vObj = $("<video>", {
id: "video_" + v.movieid,
src: v.movie_url
}).appendTo(li);
li.appendTo(p);
}
function getVideoList() {
$.each(movies, function(index, dataValue) {
makeListItem(dataValue, $(".sidebar"));
});
}
getVideoList();
});
In your script, the video list is iterated and data for each video is sent to the function. The Function thenb makes a new <li>
for each video. This will contain a <h1>
element for the title and a <video>
element. It is all then appended to the list container.
Resulting HTML
<ul class="sidebar">
<li>
<h1 class="title" for="video_10">Travel</h1>
<video id="video_10" src="http://techslides.com/demos/sample-videos/small.mp4"></video>
</li>
<li>
<h1 class="title" for="video_20">Ecommerce</h1>
<video id="video_20" src="http://techslides.com/demos/sample-videos/small.mp4"></video>
</li>
</ul>
Hope that helps.
Upvotes: 1