javascript - Jquery - change the content of a div class to details of clicked video -


i making video playlist youtube hybrid app. populate/display data using each function , append them json data thrown webserver.

i have sample json data here:

[{"video_id":"1","video_youtube_title":"this latest video", "video_youtube_description":"discription primary video.", "video_youtube_thumbnail":"https://i.ytimg.com/vi/avs4w7gzsq0/hqdefault.jpg", "video_youtube_date_published":"2016-07-15 13:37:00", "video_youtube_duration":"pt16m15s", "video_youtube_link":"avs4w7gzsq0", "video_fb_link":"example/posts/1715658608683485", "video_date_added":"2016-07-14 23:45:58"}] , [{ "video_id":"2", "video_youtube_title":"title 1", "video_youtube_description":"description1", "video_youtube_thumbnail":"https://i.ytimg.com/vi/kpdnw3_1goi/hqdefault.jpg", "video_youtube_date_published":"2016-03-07 15:00:00", "video_youtube_duration":"pt1m16s", "video_youtube_link":"kpdnw3_1goi", "video_fb_link":"example/posts/1666877406894939", "video_date_added":"2016-03-09 07:00:00"} , { "video_id":"3", "video_youtube_title":"title 2", "video_youtube_description":"description2", "video_youtube_thumbnail":"https://i.ytimg.com/vi/drje1jwddli/hqdefault.jpg", "video_youtube_date_published":"2016-03-08 16:00:00", "video_youtube_duration":"pt1m16s", "video_youtube_link":"drje1jwddli", "video_fb_link":"example/posts/1666877406894939", "video_date_added":"2016-03-09 06:00:00"}] 

so first array primary video, next array list video.

here sample code in appending data on primary video:

var primary_videolink = (data[1][0].video_youtube_link); var primary_videodescription = (data[1][0].video_youtube_description); var primary_videodatepublished = (data[1][0].video_youtube_date_published); var primary_videotitle = (data[1][0].video_youtube_title);   $('#primary-video').append('<iframe id="video" width="100%" height="auto"       src="https://www.youtube.com/embed/'+ primary_videolink + '" frameborder="0" allowfullscreen></iframe>'+     '<div class="primary-video-details-wrap">'+     '<div class="primary-videotitle"> '+primary_videotitle+' <i class="fa fa-check-square" aria-hidden="true"></i></div>'+     '<div class="primary-videodatepublished"> published on '+primary_videodatepublished +'</div>'+         '<div class="primary_videodescription">'+primary_videodescription+'</div>'+             '</div>' ); 

here sample code on appending data on video lists:

$.each(data[2], function(i, row) {   var video_link = row.video_youtube_link;   var video_img = row.video_youtube_thumbnail;   var video_title = row.video_youtube_title;   var video_description = row.video_youtube_description;   var video_duration = row.video_youtube_duration;   var video_published = row.video_youtube_date_published;    var str = "<div class='video-list-wrapper'>";    str += "<div class='video-wrap'>";    str += "<div class='left-video-info'><a href=#null onclick=document.getelementbyid('video').src='http://www.youtube.com/embed/"+video_link+"'><img src="+ video_img +" width='100%' height='auto'></a><span class='video-duration'>"+parseduration(video_duration)+"</span></div>";                     str += "<div class='right-video-info'>";    str += "<div class='video-title'>"+video_title+"</div>";    str += "<div class='video-date'>"+ video_published +" ago</div>";    str += "</div>";//right-info     str += "</div>";//video-wrap    str += '</div>';   $('#video-list').append(str);  }); 

the code works fine , displays primary video , list of video(thumbnails) below of it. when clicked of thumbnails, primary video able change according youtube url contains. working fine. how looks like.

added: jsfiddle https://jsfiddle.net/xkevin/h2hegehv/

my problem on how show details (video title, description, date etc.) of video have clicked. primary video details change too*(not video)*. there way can change details depending on video have clicked using jquery? hope understand problem. thank you!

edit: open suggestion on how can better develop video playlist or should consider make things workout. thanks.

i have wrote , revise code less possible achieve want , maybe can have :

var video_inf_array = []; $.each(data[2], function(i, row) {    var video_link = row.video_youtube_link;   var video_img = row.video_youtube_thumbnail;   var video_title = row.video_youtube_title;   var video_description = row.video_youtube_description;   var video_duration = row.video_youtube_duration;   var video_published = row.video_youtube_date_published;    var all_video_inf = {   // object carry 1 video's information     video_link : video_link,     video_img : video_img ,     video_title : video_title ,     video_description : video_description ,          // ... key value above ,and maybe u wanna simplify 'video_published : row.video_youtube_date_published'   }    var str = "<div class='video-list-wrapper'>";    str += "<div class='video-wrap'>";    str += "<div class='left-video-info'><a href=#null onclick=\"replace_to_primary("+i+")\"><img src="+ video_img +" width='100%' height='auto'></a><span class='video-duration'>"+parseduration(video_duration)+"</span></div>";                     str += "<div class='right-video-info'>";    str += "<div class='video-title'>"+video_title+"</div>";    str += "<div class='video-date'>"+ timeago(date)+" ago</div>";    str += "</div>";//right-info     str += "</div>";//video-wrap    str += '</div>';   $('#video-list').append(str);    video_inf_array.push(all_video_inf);   //push video's information array });  function replace_to_primary(index){    $('#video').attr('src',video_inf_array[index].video_link);   $('.primary-videotitle').text(video_inf_array[index].video_title);   $('.primary_videodescription').text(video_inf_array[index].video_description);    // ... handle other information above pattern .  } 

===========================================================================

sorry late provide correct answer , @ first , keep revise less possible , revise little more make there saying using onclick() bad practice , here why.

so more jquery , index() api key maybe wanna more =) here jsfiddle : https://jsfiddle.net/carr1005/pusyyen1/2/


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -