+2  A: 

Do this:

var elem = document.createElement('div');
elem.setAttribute('id', 'my_new_div');

if (document.getElementById('my_new_div')) { } //element exists in the document.
DexterW
Thanks mate, I had this on mind but was confused by possibility of having ids collision. Minute ago I developed new method by checking the element's parent var. It's not definitely new and i'm quite surprised that i haven't figured it before.
A: 

The safest way is to test directly whether the element is contained in the document:

function isInDocument(el) {
    var html = document.body.parentNode;
    while (el) {
        if (el === html) {
            return true;
        }
        el = el.parentNode;
    }
    return false;
}

var elem = document.createElement('div');
alert(isInDocument(elem));
document.body.appendChild(elem);
alert(isInDocument(elem));
Tim Down
A: 

Use compareDocumentPosition to see if the element is contained inside document. PPK has browser compatibility details and John Resig has a version for IE.

Jeffery To
I had wondered about using `compareDocumentPosition` too, but the messing around to get it working in all browsers doesn't really seem worth it when you can do domething as simple as walking up the node tree, as in my answer, which will work in pretty much every browser.
Tim Down