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

234
Vistas
Display in div content from a function which displays an array

I have a slider which will go through each function in an array functions, also I have another array with data, when I call for a function to display data in the console it shows my array[], but when I want to display the data from the array[] in div it shows undefined. What should I do?

const textContent = document.getElementById("textContent");// a div
const next = document.getElementById("next");// a button
const prev = document.getElementById("prev");// a button

const array = [
    "They both die at the end",
    "What if it's us",
    "Tuesday with Morrie",
    "Second life of Ove",
    "Flowers for Algernon",
    "The Minds of Billy Milligan",
    "The Fifth Sally",
    "The Touch",
    "Adele",
    "The Girl Before"
];

function first(){
    console.log("1");
};
function second(){
    for(item in array){
        textContent.innerHTML += `${array[item]}<br>`;
    }
};
function third(){
    console.log("3");
};
function four(){
    console.log("4");
};
function five(){
    console.log("5");
};

const functions = [
    first,
    second,
    third,
    four,
    five
];

number = 0;

function skip() {
    if(number > functions.length - 1){
        number = 0;
    }

    if(number < 0){
            number = functions.length - 1;
        }
    f = functions[number];
    textContent.innerHTML = f();
}

next.addEventListener("click", () => {
    number += 1;
    skip();
})

prev.addEventListener("click", () => {
    number -= 1;
    skip();
})

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your skip function, you are setting textContent.innerHTML to be the result of the current function. And your second function is not returning anything.

You can either return results from all the functions like this

function second(){
    let innerHTML = "";
    for(const item in array){
        innerHTML += `${array[item]}<br />`;
    }

    return innerHTML;
};

Or in your skip function, call the current function and avoid setting innerHTML to its return value.

f = functions[number];
f();

You can see a working demo here https://codepen.io/abito12/pen/RwMRqro

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