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

161
Views
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 answers
Answer question

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 Report

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 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!