Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda