Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:54:02+00:00 2026-05-28T05:54:02+00:00

How can I make this more manageable? The song elements are generated by PHP

  • 0

How can I make this more manageable? The song elements are generated by PHP so I don’t know how many there will be. The number of variables for current_song is also unknown but is the same as the song elements. Thanks…

function gid(name)
      {
        return document.getElementById(name);
      };

function itemMonitor(obj)
      {         
        var current_song = jwplayer().getPlaylistItem().index;

        gid('nowplaying').innerHTML = 'Now Playing: <span>' + player.getPlaylist()[obj.index].title + '</span>';

        if (current_song == 0) {
            gid('song0').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 0) {
            gid('song0').style.backgroundColor = "#ffffff";}

        if (current_song == 1) {
            gid('song1').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 1) {
            gid('song1').style.backgroundColor = "#ffffff";}    

        if (current_song == 2) {
            gid('song2').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 2) {
            gid('song2').style.backgroundColor = "#ffffff";}

        if (current_song == 3) {
            gid('song3').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 3) {
            gid('song3').style.backgroundColor = "#ffffff";}

        if (current_song == 4) {
            gid('song4').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 4) {
            gid('song4').style.backgroundColor = "#ffffff";}

        if (current_song == 5) {
            gid('song5').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 5) {
            gid('song5').style.backgroundColor = "#ffffff";}

        if (current_song == 6) {
            gid('song6').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 6) {
            gid('song6').style.backgroundColor = "#ffffff";}

        if (current_song == 7) {
            gid('song7').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 7) {
            gid('song7').style.backgroundColor = "#ffffff";}

        if (current_song == 8) {
            gid('song8').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 8) {
            gid('song8').style.backgroundColor = "#ffffff";}

        if (current_song == 9) {
            gid('song9').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 9) {
            gid('song9').style.backgroundColor = "#ffffff";}

        if (current_song == 10) {
            gid('song10').style.backgroundColor = "#E6E8FA";}
        else if (current_song !== 10) {
            gid('song10').style.backgroundColor = "#ffffff";}           
      };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:54:03+00:00Added an answer on May 28, 2026 at 5:54 am

    Use the following code. All song-elements are obtained through document.querySelectorAll('[id^="song"]'). Then, you loop through this collection, and set the desired property:

    function gid(name) {
        return document.getElementById(name);
    }
    
    function itemMonitor(obj) {         
        var current_song = jwplayer().getPlaylistItem().index;
        var currentPlayListItem = player.getPlaylist()[obj.index];
        if (!currentPlayListItem) {
            // The song does not exist, atm. Do something, e.g. throw an error:
            alert("Song does not exist!");
            return;
        }
    
        gid('nowplaying').innerHTML = 'Now Playing: <span>' + currentPlayListItem.title + '</span>';
    
        var all_songs = document.querySelectorAll('[id^="song"]');
        for (var i=0; i<all_songs.length; i++) {
            var song = all_songs[i];
            var songId = /^song(\d+)$/.exec(song.id);
            if (songId === null) continue; // Not a song
            else songId = 1*songId[1]; // Match the songId, convert to number
            all_songs[i].style.backgroundColor = current_song === songId ? "#E6E8FA" : "#FFF";
            // Or, replace the previous line with:
            /*if (current_song === songId) {
                all_songs[i].style.backgroundColor = "#E6E8FA";
            } else {
                all_songs[i].style.backgroundColor = "#ffffff";
            }*/
        }
     }
    

    Note regarding coding style:

    • Function declarations (function name(){}) do not have to be postfixed by a semicolon. It’s not illegal though.
    • if (a === b) { ... } else if (a !== b){..} can be shortened to if (a === b) {...} else { ... }, because if a is not equal to b, then it is unequal.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As you know the dash introduces a comment how can I make this valid?
How can I make this function more efficient. It's currently running at 6 -
Can you make this more pythonic by using the map and/or reduce functions? it
How can I make this more secure and safe: Strip unsafe characters from title
I have a very simple question: How can I make this code more simple
Ok, let's see if I can make this make sense. I have a program
How can I make this work? switch(property.PropertyType){ case typeof(Boolean): //doStuff break; case typeof(String): //doOtherStuff
How can I make this java generic cast ? public interface IField { }
How can I make this input transparent? <input type=text class=foo> I've tried this but
How can I make this sliding panel example to slide down on mouseover and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.