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

205
Views
¿Cómo puedo poner elementos "p" individuales en el div "contenedor" usando la función js?

Traté de poner elementos "p" () en los elementos "div" (contenedor) pero fallé -->

 let t = [ "First line!", "Second line!", "Third line!", "Fourth line!" ]; let T = t.length; function load(){ for(let i = 0; i < T; i++){ let contElem = document.createElement("div"); contElem.setAttribute("class", "container"); contElem.setAttribute("onload", "load02()"); document.querySelector("#main").appendChild(contElem); } } function load02(){ for(let i = 0; i < T; i++){ let myElem = document.createElement("p"); myElem.innerHTML = t[i]; document.querySelector(".container").appendChild(myElem); } }

¡Pero no funcionó! ¡Ayuda!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Su atributo onload nunca se activará... Dado que el DOM ya se ha "cargado" en este punto... Solo necesita llamar a la función... Y llamar a load2() al final de la función 1

 let t = [ "First line!", "Second line!", "Third line!", "Fourth line!" ]; let T = t.length; load(); // RUN YOUR FUNCTION function load(){ for(let i = 0; i < T; i++){ let contElem = document.createElement("div"); contElem.setAttribute("class", "container"); //contElem.setAttribute("onload", "load2()"); // REMOVE THIS document.querySelector("#main").appendChild(contElem); } load2(); //ADD THIS } function load2(){ for(let i = 0; i < T; i++){ let myElem = document.createElement("p"); myElem.innerHTML = t[i]; document.querySelector(".container").appendChild(myElem); } }
 .container{ border:1px solid #FF0000; height:50px; width:50px; }
 <div id="main"> </div>

about 4 years ago · Juan Pablo Isaza Report

0

 let t = [ "First line!", "Second line!", "Third line!", "Fourth line!" ]; let T = t.length; function load() { for (let i = 0; i < T; i++) { let contElem = document.createElement("div"), myElem = document.createElement("p"); contElem.setAttribute("class", "container"); myElem.innerHTML = t[i]; contElem.appendChild(myElem); document.querySelector("#main").appendChild(contElem); } } load();
 <div id="main"></div>

¿Eso funciona? ¿O quería poner los cuatro elementos p en cada elemento div contenedor?

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!