Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepend without jQuery? [duplicate]

Tags:

How can i write this without using jQuery?

$('body').prepend('<div id="idC" style="display: none;" class=""><div id="idA">' + titelN + '</div></div>'); 
like image 847
Alosyius Avatar asked Mar 03 '13 14:03

Alosyius


2 Answers

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]); 
like image 61
Derek Avatar answered Nov 08 '22 06:11

Derek


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>'); 
like image 25
Bergi Avatar answered Nov 08 '22 07:11

Bergi