JavaScript generate innerHTML at the start of an element

I want to generate HTML-code at the beginning of an existing element.

This is the existing HTML-element:

<div id="container">
  <p>end of the element</p>
</div>

With my current solution I add the generated element beneath the existing content:

document.getElementById(container).innerHTML += "<p>start of the element</p>";

How to add content above the existing content of an element with innerHTML?

Answer:1

Prepend document.getElementById('container').innerHTML to the assignment and remove += shorthand:

document.getElementById('container').innerHTML = "<p>start of the element</p>" + document.getElementById('container').innerHTML;
<div id="container">
  <p>end of the element</p>
</div>
Answer:2

There is a relatively new function especially for that: insertAdjacentHTML. Which has four options: before the opening tag (beforeBegin), right after the opening tag (afterBegin), just before the closing tag (beforeEnd), and after the closing tag (afterEnd). In your case:

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Demo</title>
</head>
<body>
    <div>
        <p>First paragraph in the code, but should become second after the Javascript fired.</p>
    </div>
    <script>
        document.querySelector('div').insertAdjacentHTML('afterBegin','<p>Second paragraph in the code, but should become first.</p>')
    </script>
</body>
</html>
Answer:3

This code might work

var newItem = document.createElement("p");
newItem.innerHTML = "start of the element";
var container = document.getElementById("container");
var currentItem = document.getElementById("container").firstChild;
container.insertBefore(newItem, currentItem);
Answer:4

I need to discover the function 'foo' on class Something and it doesn't work. Is this because I am missing a subtlety of using class syntax or because of babel or something else? I would prefer to use ...

I need to discover the function 'foo' on class Something and it doesn't work. Is this because I am missing a subtlety of using class syntax or because of babel or something else? I would prefer to use ...

  1. javascript functions class
  2. private functions javascript class
  3. static functions javascript class
  4. functions inside javascript class
  5. anonymous functions javascript classes
  6. javascript functions first class objects
  7. javascript functions first class
  8. object function class javascript
  9. prototype function class javascript
  10. nested functions class javascript
  11. javascript functions as first class citizens
  12. arrow functions in javascript class
  13. import function into javascript class

This is incredibly annoying.. I am wondering why the heck my changes aren't reflected as I notice that my JavaScript file for my Web Worker always gets loaded from cache: I have disabled the Cache ...

This is incredibly annoying.. I am wondering why the heck my changes aren't reflected as I notice that my JavaScript file for my Web Worker always gets loaded from cache: I have disabled the Cache ...

Tring to add interceptor header for my every request, however, it is giving me below error. Uncaught Error: [$injector:cdep] Circular dependency found: $http <- Auth <- ...

Tring to add interceptor header for my every request, however, it is giving me below error. Uncaught Error: [$injector:cdep] Circular dependency found: $http <- Auth <- ...

  1. circular dependency found angularjs
  2. circular dependency found system.uri
  3. circular dependency found $http
  4. circular dependency found
  5. circular dependency found fusion 360
  6. circular dependency found inversify
  7. circular dependency found angular
  8. cyclic dependency found
  9. cyclic dependency found sequelize
  10. angularjs circular dependency found $http

$(".className") return all the element having class .className , I want to add style only to a particular element i.e. I want to access element using their index number. <html> <head> ...

$(".className") return all the element having class .className , I want to add style only to a particular element i.e. I want to access element using their index number. <html> <head> ...

  1. css style single element
  2. html style single element
  3. wpf style single element