javascript : Object.isNodeList
javascript : Object.isNodeList
Target:
Applicable with any javascript Object Variable that holds DOM Elements List.
Description :
When an object holds an html domElementsList/NodeList/htmlCollection then it return true otherwise undefined. basically it is defined in NodeList/htmlCollection prototype. Many time in programming we face a problem that an object variable holds DomElementList or other object structure then it help us. Before writing the code of DomElementList manipulation we can check whether a variable holds domElementsList/NodeList/htmlCollection or not.
Note: It is an property of DomElementsList that is defined in NodeList/htmlCollection Prototype.
It is useful to check an object variable is DomElementsList or any other type.
When we check the typeof(object_variable) it return 'object' not 'NodeList/htmlCollection'.
Syntax:
Object.isNodeList
return true when object hold's NodeList/htmlCollection otherwise undefined
Usase:
- var obj = document.querySelector('div')
if(obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object is not a DomElementsList". - var obj = document.querySelectorAll('div')
if(obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object holds DomElementsList". - var obj = document.getElementById('firstDiv')
if(obj && obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object is not a DomElementsList". - var obj = document.getElementsByTagName('div')
if(obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object holds DomElementsList". - var obj = document.getElementsByClassName('myClass')
if(obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object holds DomElementsList". - var obj = [];
var a = document.getElementById('firstDiv');
obj.push(a);
var b = document.getElementById('secondDiv');
obj.push(b);
if(obj.isNodeList){
console.log('Object holds DomElementsList');
}
else{
console.log('Object is not a DomElementsList');
}
It return "Object is not a DomElementsList".
Download MYJS.js
Comments
Post a Comment