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

191
Visualizações
why document.addEventListener() is not working in javascript

i am trying to execute the document.addEventListener() but it is not working why ?

<html>
<head>
    <title>javascript</title>
    <link rel="stylesheet" href="C:\Users\SUDARSHAN\Desktop\html_UI\eventstyle.css">
</head>

<body>
    <p id="p1">Welcome</p>
</body>
<script>
  
function f1() {
    document.getElementById("p1").style.fontSize="50px";
}

document.addEventListener("load",f1);
console.log("hello");

</script>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You must append a load listener to window instead of document.

window.addEventListener("load",f1);

That being said, you cannot have a script element as a child of the html element, it must be in the head or body (which are the only allowed children of html). Always make sure your HTML is valid at all times; invalid HTML puts you in unspecified territory and tends to make things unpredictable (even if things seem to work).

about 4 years ago · Juan Pablo Isaza Relatório

0

Flaws in above code.

  1. Always make sure the html tag documented correctly. For ref https://www.w3schools.com/tags/tag_doctype.asp
  2. load event depends on window. In above code, you added the listener for document.
about 4 years ago · Juan Pablo Isaza Relatório

0

You can try following:

document.addEventListener('DOMContentLoaded', f1);

There are two events which are related to loading and unloading of the Document

  1. DOMContentLoaded
  2. readystatechange

Refer this page for windows events https://developer.mozilla.org/en-US/docs/Web/API/Document#load_unload_events

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