Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
I want to change song name by using forEach method but it doesn't change and doesn't show any error

I want to do basic operation like update with array of document. In this html I have song List (song-container) that contains song Item class. I want to change each song name and I tried below code but I did not received desire output.

let songItem = Array.from(document.querySelector(".songItem"));
let songs = [
    {
        Name: "love  you zindagi",
        filePath: "audio/song1.mp3",
        coverPath: "covers/download.jpg",
    },
    {
        Name: "zara-zara",
        filePath: "audio/song5.mp3",
        coverPath: "covers/download.jpg",
    },
    {
        Name: "vaaste",
        filePath: "audio/song8.mp3",
        coverPath: "covers/download.jpg",
    },
];

console.log(songs);

songItem.forEach((element, i) => {
    console.log(element, i);
    element.getElementsByClassName("songName")[0].innerText = songs[i].Name;
});
<div class="songList">
    <h1>Best Song Collection</h1>
    <div class="songItem">
        <span class="image">
            <img src="covers/download.jpg" />
        </span>
        <span class="songName">love you zindagi</span>
        <span class="timePlay"
            ><i class="far fa-play-circle" onclick="songPlay ()"></i
        ></span>
        <span class="timeStop"
            ><i class="far fa-pause-circle" onclick="pauseSong ()"></i
        ></span>
    </div>
    <div class="songItem">
        <span class="image">
            <img src="covers/download.jpg" />
        </span>
        <span class="songName">love you zindagi</span>
        <span class="timePlay"><i class="far fa-play-circle"></i></span>
    </div>
    <div class="songItem">
        <span class="image">
            <img src="covers/download.jpg" />
        </span>
        <span class="songName">love you zindagi</span>
        <span class="timePlay"><i class="far fa-play-circle"></i></span>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

querySelector returns the first element matching the selector. Use querySelectorAll instead to get an iterable of all matched elements.

let songItem = Array.from(document.querySelectorAll(".songItem"));
let songs = [
    {
        Name: "love  you zindagi",
        filePath: "audio/song1.mp3",
        coverPath: "covers/download.jpg",
    },
    {
        Name: "zara-zara",
        filePath: "audio/song5.mp3",
        coverPath: "covers/download.jpg",
    },
    {
        Name: "vaaste",
        filePath: "audio/song8.mp3",
        coverPath: "covers/download.jpg",
    },
];

console.log(songs);

songItem.forEach((element, i) => {
    console.log(element, i);
    element.getElementsByClassName("songName")[0].innerText = songs[i].Name;
});
about 4 years ago · Juan Pablo Isaza Report

0

As mentioned in the previous answers, this line

let songItem = Array.from(document.querySelector(".songItem"));

returns only the first element with class name "songItem". In order to return all of them you need to use querySelectorAll. Also, you don't have to use Array.from(). Therefore, you can use this line instead:

let songItem = document.querySelectorAll(".songItem");

On the other hand, in the last line, using element.getElementsByClassName("songName")[0] might not be the best practice, since "element" has only one child element with class name "songName", you can use querySelector in this case; like this:

element.querySelector(".songName").innerText = songs[i].Name;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!