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

Craig Kempster
Craig Kempster
4,332 Points

Song Playlist - On end of Song move active class to next song in the list

Writing a webapp that will make the active song display the color red

HTML

<div class="songList"> <p id="active">1. Song</p> <p>2. Song</p> <p>3. Song</p> <p>4 .Song</p> <p>5. Song</p> <p>6. Song</p> <p>7. Song</p> <p>8. Song</p> <p>9. Song</p> <p>10. Song</p> </div>

JAVASCRIPT

// Define variables let song_list = document.getElementsByClassName('songList'); let activeSong = song_list.querySelector('.active'); activeSong.style.cssText = 'color: red';

function selectActiveSong() { // loop to next song in the player for (var i = 0; i < activeSong.length; ) { activeSong[i].classList.remove('active'); activeSong[i].style.cssText = 'color: white'; i++ } // Add active to next song in the player activeSong[i].classList.add('active'); activeSong[i].style.cssText = 'color: red';

}

selectActiveSong();

1 Answer

Craig Kempster
Craig Kempster
4,332 Points

After hours of research and yelling at the console - had to use JQuery...... Not sure if there is a better way of doing the code but this works

JSfile

  // Define variables
  let activeSong = document.getElementById("active");
  activeSong.style.cssText = 'color: red';
  let getSongList = document.querySelector("p");

// Declare function which gets the active song
function selectActiveSong() {

  // remove current active song
  $("#active").removeAttr("id style");     
  // loop to next song in the playerlist
  getSongList = getSongList.nextElementSibling;
  // Add active id to next song
  getSongList.setAttribute("id", "active");
  // Set active song in playlist to color red
  getSongList.style.cssText = 'color: red';  

}