Orbie
Orbie

Reputation: 7

Why do I only receive a single alert from the following javascript?

This is a javascript function inside of a HTML tag, however - when I move the alert(currentalbum) below the for loop, the second alert does not run - only the first, why?

function populatetracks(albumvalue) {
    var currentalbum = albumvalue;
    alert(currentalbum); // #1

    document.getElementById("TracksList").options.length = 0; 

    for(i = 0; albums[albumvalue].tracks.length - 1; i++) {
        var s = document.getElementById('TracksList');
        var opt = document.createElement('option');
        opt.appendChild( document.createTextNode(albums[albumvalue].tracks[i].title));
        opt.value = i;
        s.appendChild(opt);
    }

    alert(currentalbum); // #2
}

'#1' does produce an alert, but '#2' doesn't.

Upvotes: 0

Views: 49

Answers (1)

DDeMartini
DDeMartini

Reputation: 337

As noted, not sure how your for loop is supposed to stop.

This has no evaluation in it, just an incrementor

for(i=0; albums[albumvalue].tracks.length -1; i++){

Maybe try this (might need to change '=' to '<=' )

for(i=0; i < albums[albumvalue].tracks.length -1; i++){

Upvotes: 2

Related Questions