Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda