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

279
Views
elementos constantes definidos en el módulo js: no se puede acceder porque el contenido DOM aún no se ha cargado

Usando el siguiente ejemplo. El elemento const test_el es nulo porque el contenido DOM aún no está cargado. Entonces test() genera un error.

Envolver la definición constante en un evento de carga no parece funcionar ya que ahora está en el mismo alcance que la función.

¿Cuál es el método correcto para lograr esto? Sé que puedo agregar la const dentro de la función, pero debe reutilizarse en varias funciones (no en este ejemplo)

principal.js

 import {test} from "../import.js" window.addEventListner("load", ()=>{ test() })

importar.js

 const test_el = document.getElementById("test_div") export function test(){ console.log(test_el.innerText) }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

// you can call // all the tests that are dependent of `test_el` from one parent function // after getting value of `test_el` export function runTests() { const test_el = document.getElementById("test_div"); if(test_el) { firstTest(test_el) secondTest(test_el) } else { throw new Error('Element not found.') } } function firstTest(element){ assert(element.innerText.equal('someText')) } function secondTest(element){ assert(element.classList.includes('someClass')) }
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!