The isConnected property returns a boolean value indicating whether the node is connected (directly or indirectly) to the context object or the document. This is a read-only property.
Syntax:
check = node.isConnected
Return Value:
- true: If the node is connected to the document.
- false: If the node is not connected to the document.
Example 1: This property returns false.
HTML
<!DOCTYPE html> < html >
< head >
< meta charset = "utf-8" >
< title >HTML DOM Node isConnected property</ title >
</ head >
< body >
< h1 >GeeksforGeeks</ h1 >
< p >Click to Check</ p >
< button onclick = "Check()" >Click</ button >
< script >
function Check() {
let exp = document.createElement('p');
exp.innerHTML = "abcd";
a = exp.isConnected;
console.log(a); // Returns false
if (a == false) {
console.log("Not Connected");
}
else {
console.log("Connected")
}
}
</ script >
</ body >
</ html >
|
Output:
Before Clicking the Button:
After Clicking the Button:
Example 2: This property returns true.
HTML
<!DOCTYPE html> < html >
< head >
< meta charset = "utf-8" >
< title >HTML DOM Node isConnected property</ title >
</ head >
< body >
< h1 >GeeksforGeeks</ h1 >
< p >Click to Check</ p >
< button onclick = "Check()" >Click</ button >
< script >
function Check() {
let exp = document.createElement('p');
exp.innerHTML = "abcd";
document.body.appendChild(exp);
a = exp.isConnected;
console.log(a); // Returns true
if (a == false) {
console.log("Not Connected");
}
else {
console.log("Connected")
}
}
</ script >
</ body >
</ html >
|
Output:
Before Clicking the Button:
After Clicking the Button:
Supported Browsers:
- Google Chrome 51
- Edge 79
- Firefox 49
- Safari 10
- Opera 38