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";
document.body.appendChild(div);
alert(div.parentNode); //body element

document.body.removeChild(div);
alert(div.parentNode); //null

No comments:

Subscribe in a Reader