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! While you're at it, check out some resources Treehouse students have shared here.

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

fisnik poroshtica
fisnik poroshtica
14,154 Points

Can somebody tell me what this function does?

Can somebody tell me what this function does? i dont understand list argument where it come from?

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

2 Answers

Steven Parker
Steven Parker
229,644 Points

:point_right: This function causes the list of songs to appear in the HTML document.

Since this is a method of a Playlist object, it will loop through the songs that are part of the playlist that it is called on. The playlist itself is represented inside the function as "this". Each song name will be converted to HTML and added to the document element that it is given as the argument ("list").

You can see it being demonstrated at about time index 04:40 of the Making the UI Work video.

The "list" argument passed to the function in the video is playlistElement, which was assigned by a call to document.GetElementById and refers to the ordered list (<ol>) element on the page.

Ivan Sandoval
Ivan Sandoval
961 Points

Basically the renderInElement(list) method is the one who displays all the songs on the HTML document.

The argument that it receives represents this HTML element
<ol id="playlist"> </ol>

Now, from the documentation: The innerHTML property sets or returns the HTML content (inner HTML) of an element.

list.innerHTML = "";

And finally, in the for loop you're adding to this element each one of the songs represented as an HTML String

for(var i = 0; i < this.songs.length; i++) { list.innerHTML += this.songs[i].toHTML(); }