Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Making the UI Work

Kenan Xin
Kenan Xin
16,139 Points

Re: return dom element instead of html

Hi, in the song.js, i tried to modify the toHTML() method to return a dom object rather than a String, but i have no idea how to make it work next. Anyone could help me?

song.js

function Song (title, artist, duration){
     this.title     = title;
     this.artist    = artist;
     this.duration  = duration;
     this.isPlaying = false;
}

Song.prototype.play = function(){
    this.isPlaying = true;
}

Song.prototype.stop = function() {
    this.isPlaying = false;
}

Song.prototype.toHTML = function(){
    var listItem = document.createElement("li");
    var span = document.createElement("span");
    var titleTxt = document.createTextNode(
        this.title +  " - " + this.artist
        );
    var spanTxt = document.createTextNode(this.duration);

    span.appendChild(spanTxt);
    listItem.appendChild(titleTxt);
    listItem.appendChild(span);

    if (this.isPlaying === true){
        listItem.className = "current";
    }else{
        listItem.className ="";
    }

    return listItem;
}

playlist.js

function Playlist(){
    this.songs=[];
    this.currentSongIndex = 0;
}

Playlist.prototype.add = function(song){
    this.songs.push(song);
}

Playlist.prototype.play = function(){
    this.songs[currentSongIndex].play();
}


Playlist.prototype.stop = function(){
    this.songs[currentSongIndex].stop();
}


Playlist.prototype.next = function(){
    this.stop();
    this.currentSongIndex++;
    if (this.currentSongIndex === this.songs.length)
        this.currentSongIndex = 0;
    this.play();
}

Playlist.prototype.insertInto = function(element){
    // ????????????????????????????

}

app.js

var playList = new Playlist();

var playButton = document.getElementById("play");
var nextButton = document.getElementById("Next");
var stopBuutton = document.getElementById("stop");
var playlistContainer = document.getElementById("playlist");

var hereComesTheSun = new Song("Here Comes the Sun", "The Beatles", "2:54");
var walkingOnSunshine = new Song("Walking on Sunshine", "Katrina and the Waves", "3:43");

playList.add(hereComesTheSun);
playList.add(walkingOnSunshine);

playList.insertInto(playlistContainer);

playButton.onclick = function(){
    playList.play();
    playList.insertInto(playlistContainer);
}
Kenan Xin
Kenan Xin
16,139 Points

Okay i found the answer myself after a shower.

song.js

function Song (title, artist, duration){
     this.title     = title;
     this.artist    = artist;
     this.duration  = duration;
     this.isPlaying = false;
}

Song.prototype.play = function(){
    this.isPlaying = true;
}

Song.prototype.stop = function() {
    this.isPlaying = false;
}

Song.prototype.toHTML = function(){
    var listItem = document.createElement("li");
    var span = document.createElement("span");
    var titleTxt = document.createTextNode(
        this.title +  " - " + this.artist
        );
    var spanTxt = document.createTextNode(this.duration);

    span.appendChild(spanTxt);
    listItem.appendChild(titleTxt);
    listItem.appendChild(span);

    if (this.isPlaying === true){
        listItem.className = "current";
    }else{
        listItem.className ="";
    }

    return listItem;
}

playlist.js

function Playlist(){
    this.songs=[];
    this.currentSongIndex = 0;
}

Playlist.prototype.add = function(song){
    this.songs.push(song);
}

Playlist.prototype.play = function(){
    this.songs[this.currentSongIndex].play();
}


Playlist.prototype.stop = function(){
    this.songs[this.currentSongIndex].stop();
}


Playlist.prototype.next = function(){
    this.stop();
    this.currentSongIndex++;
    if (this.currentSongIndex === this.songs.length)
        this.currentSongIndex = 0;
    this.play();
}

Playlist.prototype.insertInto = function(element){
    element.innerHTML= "";
    var list;
    for (var i = 0 ; i < this.songs.length; i++){
        list = this.songs[i].toHTML();
        element.appendChild(list);
    }

    // ????????????????????????????

}

app.js

var playList = new Playlist();

var playButton = document.getElementById("play");
var nextButton = document.getElementById("next");
var stopButton = document.getElementById("stop");

var playlistContainer = document.getElementById("playlist");

var hereComesTheSun = new Song("Here Comes the Sun", "The Beatles", "2:54");
var walkingOnSunshine = new Song("Walking on Sunshine", "Katrina and the Waves", "3:43");

playList.add(hereComesTheSun);
playList.add(walkingOnSunshine);

playList.insertInto(playlistContainer);

playButton.onclick = function(){
    playList.play();
    playList.insertInto(playlistContainer);
}

nextButton.onclick = function(){
    playList.next();
    playList.insertInto(playlistContainer);
}

stopButton.onclick = function(){
    playList.stop();
    playList.insertInto(playlistContainer);
}