Monday, May 12, 2008

Detecting whether the element exists in the browser if we have the reference of that element

Suppose you have a reference to an element and you want to make sure this element does exists on the browser document. You can detect this by checking the parentNode property of that element.

var div = document.createElement("div");
div.innerHTML = "demo";
alert(div.parentNode); //body element

alert(div.parentNode); //null

No comments:

Subscribe in a Reader