25

How can i write this without using jQuery?

$('body').prepend('<div id="idC" style="display: none;" class=""><div id="idA">' + titelN + '</div></div>');
1
  • 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. Commented Feb 6, 2014 at 12:00

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

Comments

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>');

1 Comment

Actually it doesn't work in older versions of most browsers... but they're REALLY old versions (see developer.mozilla.org/en-US/docs/Web/API/…) ;) - 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.