mercredi 4 novembre 2015

Multiple if statements- all or nothing?

I'm trying to load specific values, out of what will someday be a larger JSON response (videos on Wistia). For now, it only has two videos (slatwall.mp4 & igoodworks.mp4) and their respective data as seen in the json HERE.

The javascript below works great *if both of the v.name values exist. But, if you change slatwall.mp4 to something else, neither if statements work, and the else statement is called.. even though igoodworks.mp4 exists.

Javascript (JSFiddle is also below):

var projectUrl = "http://ift.tt/1XRAIkB"

$.getJSON(projectUrl, function (data) {

$('#thumb').show();
$('#thumb2').show();

$.each(data.medias, function (i, v) {

    var thumb_orig = v.thumbnail.url;
    var thumb_size = "640x360";
    var thumb = thumb_orig.replace('100x60', thumb_size);
    var duration = v.duration;
    var name = v.name;
    var created = v.created;
    var status = v.status;

    if (v.name == "slatwall.mp4") {
        $('#thumb').attr('src', thumb);
        $('#duration').text(duration + " seconds long");
        $('#status').text(name);
        return;
    } 
    if (v.name == "igoodworks.mp4") {
        $('#thumb2').attr('src', thumb);
        $('#duration2').text(duration + " seconds long");
        $('#status2').text(name);
        return;
    } else {
        $('body').html('No Videos Found');
    }
    })
});

Any help what I'm trying to accomplish?

JSFiddle

Aucun commentaire:

Enregistrer un commentaire