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

163
Vistas
How can I append an element after another in the document head?

I need to inject style elements into the head section of my html using JavaScript. For some context I am using webpack to inject styles. I have a function that is supposed to inject styles at an id inside of my head tags.

This is what my html looks like:

function insertAtElement(element) {
  var target = document.getElementById(
    "inject-theme-styles-here"
  );
  target.appendChild(element);
}
<head>
  <meta charset="UTF-8" />
  <title>Webpack tutorial</title>
  <noscript id="inject-theme-styles-here"></noscript>
</head>

<body>
  <div id="root"></div>
</body>

I am not getting my desired output. In the element tab in chrome it adds it inside the tag. But I need it to be added right below it not inside of it.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want your element to be added after target, instead of using target.appendChild(element); which appends a child element to target, use the following:

target.parentNode.insertBefore(element, target.nextSibling);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Create manually a script, set the src attribute and put it inside the head using tag name (or querySelector if you wish)

// create a script element
let theScript = document.createElement("script");

// define it as javascript script
theScript.setAttribute("type","text/javascript");

// set the src
theScript.setAttribute("src","src/myscript.js");

// insert into the head
document.getElementsByTagName("head")[0].appendChild(theScript);    

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