Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda