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

156
Visualizações
My array (myLeads) elements are not rendered in HTML list but they are showing correctly in console. I want to print myLeads[ ] elements in HTML list

let myLeads = [];
const inputEl = document.getElementById("input-el");
const inputBtn = document.getElementById("input-btn");
const ulEl = document.getElementById("ul-el");

inputBtn.addEventListener("click", function(){
    myLeads.push(inputEl.value);
    console.log(myLeads)
})

let arr = [2, 3, "fadf"]

/*arr[] elements are rendered correctly in HTML list. But myLeads[] elements are not rendered in HTML list, as they are printed correctly in console.*/

for(let i = 0; i < myLeads.length; i++){
    ulEl.innerHTML += "<li>" + myLeads[i] + "</li>"
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <title>save leads</title>
    <link rel="stylesheet" href="/index.css">
</head>
<body>
    <input type="text" id="input-el">
    <button id="input-btn">Save tabs</button>
    <ul id="ul-el"></ul>
    <script src="/index.js"></script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Put the loop in a function and call it. Also make sure to reset the ul to be empty

let myLeads = [];
const inputEl = document.getElementById("input-el");
const inputBtn = document.getElementById("input-btn");
const ulEl = document.getElementById("ul-el");

inputBtn.addEventListener("click", function(){
    myLeads.push(inputEl.value);
    updateList();
})

let arr = [2, 3, "fadf"]

/*arr[] elements are rendered correctly in HTML list. But myLeads[] elements are not rendered in HTML list, as they are printed correctly in console.*/
function updateList(){
    ulEl.innerHTML = "";
    for(let i = 0; i < myLeads.length; i++){
        ulEl.innerHTML += "<li>" + myLeads[i] + "</li>"
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <title>save leads</title>
    <link rel="stylesheet" href="/index.css">
</head>
<body>
    <input type="text" id="input-el">
    <button id="input-btn">Save tabs</button>
    <ul id="ul-el"></ul>
    <script src="/index.js"></script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

You are not updating the HTML element anywhere when the click event is being fired. That means your list is only populated by the initial elements of myLeads when the page is refreshed and then never again.

Move the cycle for generating li elements into a function, for example

function updateList(arr){
    ulEl.innerHTML = "";
    for(let i = 0; i < arr.length; i++){
        ulEl.innerHTML += "<li>" + myLeads[i] + "</li>"
    }
}

and then call it inside the event handler

inputBtn.addEventListener("click", function(){
    myLeads.push(inputEl.value);
    console.log(myLeads)
    updateList(myLeads);
})
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