25

How can i write this without using jQuery?

$('body').prepend('<div id="idC" style="display: none;" class=""><div id="idA">' + titelN + '</div></div>');
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • 6
    I think this question stands for itself. It's only slightly "related" to the one marked duplicate (and the marked answer doesn't answer this one. – Matthew Wilcoxson Feb 06 '14 at 12:00

2 Answers2

23

You should first learn how to create an element without using any library. Basically, there are some method for you to create element and append element to the document. They are createElement, insertBefore, appendChild.

createElement ref: http://www.w3schools.com/jsref/met_document_createelement.asp
insertBefore ref: http://www.w3schools.com/jsref/met_node_insertbefore.asp
appendChild ref: http://www.w3schools.com/jsref/met_node_appendChild.asp

The code below should work for all browser. Try more and learn more.

var parent = document.createElement("div");
parent.id = "idC";
parent.style.display = "none";

var child = document.createElement("div");
child.id = "idA";
child.innerHTML = titelN;
parent.appendChild(child);

document.body.insertBefore(parent,document.body.childNodes[0]);
Derek
  • 2,695
  • 1
  • 16
  • 17
16

There is the insertAdjacentHTML method, but it does not work in older FF versions:

document.body.insertAdjacentHTML('afterbegin', '<div id="idC" style="display: none;" class=""><div id="idA">' + titelN + '</div></div>');
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Actually it doesn't work in older versions of most browsers... but they're REALLY old versions (see https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML) ;) - it's also a HTML5 standard now. However, if that does worry then don't be afraid to stick with jQuery - that's precisely what it's for. – Matthew Wilcoxson Feb 06 '14 at 12:03