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:

  1. 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".
  2. 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".
  3. 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".
  4. 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".
  5. 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".
  6. 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

MyJs is a collection of java script extension methods that enhances the JavaScript objects, class object, DOM element, domElementsCollections, Date, String, Number and other data type. 

Include in you project landing page (master page) and it will extends your DOM and JavaScript Objects.

Comments

Popular posts from this blog

javascript : String.right

javascript : String.left

javascript : .nextAll()