Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set DOM element as the first child?

I have element E and I'm appending some elements to it. All of a sudden, I find out that the next element should be the first child of E. What's the trick, how to do it? Method unshift doesn't work because E is an object, not array.

Long way would be to iterate trough E's children and to move'em key++, but I'm sure that there is a prettier way.

like image 723
Popara Avatar asked Jan 05 '10 16:01

Popara


People also ask

How do I add a div to my first child?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.


1 Answers

var eElement; // some E DOM instance var newFirstElement; //element which should be first in E  eElement.insertBefore(newFirstElement, eElement.firstChild); 
like image 81
nemisj Avatar answered Nov 02 '22 23:11

nemisj