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

393
Visualizações
What is the point of getElementById when JS finds elements by name anyway

If there is an HTML element with id="hello123", then this JS code prints the content of the element: console.log(hello123.innterHTML)

console.log(hello123.innerHTML);
<p id="hello123">hello this works 123</p>

I discovered this by happenstance, I would other wise have written:

hello123 = document.getElementById("hello123);
console.log(hello123.innerHTML);

What is the difference between the two? Is using getElementById better because it protects be in the case that hello123 is a variable already defined elsewhere that has nothing to do with an HTML element called hello123?

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

0

Elements with an id become Global properties of the window object in the Browser's Object Model (BOM). It is not "JavaScript" that makes this happen. Accessing an element with getElementById() uses the Document Object Model (DOM) API, which is standardized. The BOM is largely not standardized. Using the DOM is the best practice.

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