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

164
Views
Los elementos de mi matriz (myLeads) no se representan en la lista HTML, pero se muestran correctamente en la consola. Quiero imprimir los elementos myLeads[ ] en la lista HTML

 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 answers
Answer question

0

Pon el bucle en una función y llámalo. También asegúrese de restablecer el ul para que esté vacío

 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 Report

0

No está actualizando el elemento HTML en ninguna parte cuando se activa el evento de clic. Eso significa que su lista solo se completa con los elementos iniciales de myLeads cuando se actualiza la página y nunca más.

Mueva el ciclo para generar elementos li en una función, por ejemplo

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

y luego llamarlo dentro del controlador de eventos

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