javascript prependChild
Filed in Development on Feb.17, 2009
You might know appendChild method which allows you to add an element to the end of another. Like this:
someParentObject = document.getElementById("someid");
...
someChildObject = document.createElement("div");
someChildObject.innerHTML = 'Content';
// append the someChildObject to the end of someParentObject
someParentObject.appendChild(someChildObject);
But what to do if you want to add the child object to the very beginning of parent object? I noticed many people are searching for prependChild method. Strange, but… there is no such method.:) Yet there is another trick:
someParentObject.appendChild(someChildObject,someParentObject.firstChild);
I hope it will help anybody.;-)
Related posts:
Tags: javascript


March 16th, 2009 at 3:50 am
Nice trick. Works a little better like this -
someParentObject.insertBefore(someChildObject,someParentObject.firstChild);